Bug 849847 - Make about:home scrollable with the analog stick. r=Cwiiis

This commit is contained in:
Brian Nicholson 2013-06-24 21:44:07 -07:00
Родитель 1727aad1db
Коммит e74572c8bc
4 изменённых файлов: 102 добавлений и 2 удалений

Просмотреть файл

@ -411,9 +411,9 @@ abstract public class BrowserApp extends GeckoApp
// put the focus on the layerview and carry on
if (mLayerView != null && !mLayerView.hasFocus() && GamepadUtils.isPanningControl(event)) {
if (mAboutHome.getUserVisibleHint()) {
mLayerView.requestFocus();
} else {
mAboutHome.requestFocus();
} else {
mLayerView.requestFocus();
}
}
return false;

Просмотреть файл

@ -148,6 +148,7 @@ FENNEC_JAVA_FILES = \
RemoteTabs.java \
RobocopAPI.java \
ServiceNotificationClient.java \
ScrollAnimator.java \
SessionParser.java \
SetupScreen.java \
ShapedButton.java \

Просмотреть файл

@ -0,0 +1,84 @@
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko;
import java.util.Timer;
import java.util.TimerTask;
import org.mozilla.gecko.util.GamepadUtils;
import android.view.InputDevice;
import android.view.MotionEvent;
import android.view.View;
public class ScrollAnimator implements View.OnGenericMotionListener {
private Timer mScrollTimer;
private int mX;
private int mY;
// Assuming 60fps, this will make the view scroll once per frame
static final long MS_PER_FRAME = 1000 / 60;
// Maximum number of pixels that can be scrolled per frame
static final float MAX_SCROLL = 0.075f * GeckoAppShell.getDpi();
private class ScrollRunnable extends TimerTask {
private View mView;
public ScrollRunnable(View view) {
mView = view;
}
@Override
public final void run() {
mView.scrollBy(mX, mY);
}
}
@Override
public boolean onGenericMotion(View view, MotionEvent event) {
if ((event.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
// Cancel the animation if the joystick is in a neutral position
if (GamepadUtils.isValueInDeadZone(event, MotionEvent.AXIS_X) &&
GamepadUtils.isValueInDeadZone(event, MotionEvent.AXIS_Y)) {
if (mScrollTimer != null) {
mScrollTimer.cancel();
mScrollTimer = null;
}
return true;
}
// Scroll with a velocity relative to the screen DPI
mX = (int) (event.getAxisValue(MotionEvent.AXIS_X) * MAX_SCROLL);
mY = (int) (event.getAxisValue(MotionEvent.AXIS_Y) * MAX_SCROLL);
// Start the timer; the view will continue to scroll as long as
// the joystick is not in the deadzone.
if (mScrollTimer == null) {
mScrollTimer = new Timer();
ScrollRunnable task = new ScrollRunnable(view);
mScrollTimer.scheduleAtFixedRate(task, 0, MS_PER_FRAME);
}
return true;
}
}
return false;
}
/**
* Cancels the running scroll animation if it is in progress.
*/
public void cancel() {
if (mScrollTimer != null) {
mScrollTimer.cancel();
mScrollTimer = null;
}
}
}

Просмотреть файл

@ -10,11 +10,13 @@ import java.util.EnumSet;
import org.mozilla.gecko.GeckoApplication;
import org.mozilla.gecko.LightweightTheme;
import org.mozilla.gecko.R;
import org.mozilla.gecko.ScrollAnimator;
import org.mozilla.gecko.db.BrowserContract;
import android.app.Activity;
import android.content.res.Configuration;
import android.database.ContentObserver;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.ContextMenu.ContextMenuInfo;
@ -43,6 +45,7 @@ public class AboutHome extends Fragment {
private LastTabsSection mLastTabsSection;
private RemoteTabsSection mRemoteTabsSection;
private TopSitesView mTopSitesView;
private ScrollAnimator mScrollAnimator;
public interface UriLoadListener {
public void onAboutHomeUriLoad(String uriSpec);
@ -103,6 +106,13 @@ public class AboutHome extends Fragment {
mAboutHomeView.setLightweightTheme(mLightweightTheme);
mLightweightTheme.addListener(mAboutHomeView);
// ScrollAnimator implements the View.OnGenericMotionListener
// interface, which was added in API level 12.
if (Build.VERSION.SDK_INT >= 12) {
mScrollAnimator = new ScrollAnimator();
mAboutHomeView.setOnGenericMotionListener(mScrollAnimator);
}
return mAboutHomeView;
}
@ -137,6 +147,11 @@ public class AboutHome extends Fragment {
getActivity().getContentResolver().unregisterContentObserver(mTabsContentObserver);
mTopSitesView.onDestroy();
if (mScrollAnimator != null) {
mScrollAnimator.cancel();
}
mScrollAnimator = null;
mAboutHomeView = null;
mAddonsSection = null;
mLastTabsSection = null;