android 模拟滑动事件,见下面isSlideUnlockScreen()代部分.
package com.android.internal.policy.impl.keyguard;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.media.IAudioService;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.telephony.TelephonyManager;
import android.util.AttributeSet;
import android.util.Log;
import android.util.Slog;
import android.view.KeyEvent;
import android.widget.FrameLayout;
//kaka
import android.view.MotionEvent;
import android.os.SystemClock;
import android.hardware.input.InputManager;
import android.view.InputEvent;
import android.os.Handler;
import android.os.Message;
import android.view.KeyCharacterMap;
import android.os.Binder;
import android.view.InputDevice;
//kaka
import android.provider.Settings;//kaka
/**
* Base class for keyguard view. {@link #reset} is where you should
* reset the state of your view. Use the {@link KeyguardViewCallback} via
* {@link #getCallback()} to send information back (such as poking the wake lock,
* or finishing the keyguard).
*
* Handles intercepting of media keys that still work when the keyguard is
* showing.
*/
public abstract class KeyguardViewBase extends FrameLayout {
private static final int BACKGROUND_COLOR/* = 0x70000000*/;//kaka
private AudioManager mAudioManager;
private TelephonyManager mTelephonyManager = null;
protected KeyguardViewMediator.ViewMediatorCallback mViewMediatorCallback;
//kaka
private static final String KEY_LOCKSCREEN_PAPER = "choose_lock_paper";
private static final String RESOURCE_PKG = "com.android.launcher";
private static final String RESOURCE_TYPE = "drawable";
static{
if("Q".equals(android.util.QpFeatures.QP_PROJ_NAME)){
BACKGROUND_COLOR = 0x0;
}else if("QQ".equals(android.util.QpFeatures.QP_PROJ_NAME)){
BACKGROUND_COLOR = 0x00000000;
}else{
BACKGROUND_COLOR = 0x70000000;
}
}
//kaka
// Whether the volume keys should be handled by keyguard. If true, then
// they will be handled here for specific media types such as music, otherwise
// the audio service will bring up the volume dialog.
private static final boolean KEYGUARD_MANAGES_VOLUME = true;
// This is a faster way to draw the background on devices without hardware acceleration
/// M: Do not make it static, or memory leak may happen
private static final Drawable mBackgroundDrawable = new Drawable() {
@Override
public void draw(Canvas canvas) {
canvas.drawColor(BACKGROUND_COLOR, PorterDuff.Mode.SRC);
}
@Override
public void setAlpha(int alpha) {
}
@Override
public void setColorFilter(ColorFilter cf) {
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
};
public KeyguardViewBase(Context context) {
this(context, null);
}
public KeyguardViewBase(Context context, AttributeSet attrs) {
super(context, attrs);
resetBackground();
}
public void resetBackground() {
//kaka
try {
String str = Settings.System.getString(getContext().getContentResolver(), KEY_LOCKSCREEN_PAPER);
if(str == null){
str = "default_lockscreen_wallpaper";
}
Context remContext = mContext.createPackageContext(RESOURCE_PKG, Context.CONTEXT_IGNORE_SECURITY);
if(remContext.getResources().getBoolean(
remContext.getResources().getIdentifier("config_enable_lockscreen_wallpaper", "bool", RESOURCE_PKG))){
final int id = remContext.getResources().getIdentifier(str, RESOURCE_TYPE, RESOURCE_PKG);
Drawable drawable = remContext.getResources().getDrawable(id);
setBackground(drawable);
}else{
setBackground(mBackgroundDrawable);
}
} catch (Exception e) {
}
//kaka
}
/**
* Called when you need to reset the state of your view.
*/
abstract public void reset();
/**
* Called when the screen turned off.
*/
abstract public void onScreenTurnedOff();
/**
* Called when the screen turned on.
*/
abstract public void onScreenTurnedOn();
/**
* Called when the view needs to be shown.
*/
abstract public void show();
/**
* Called when a key has woken the device to give us a chance to adjust our
* state according the the key. We are responsible for waking the device
* (by poking the wake lock) once we are ready.
*
* The 'Tq' suffix is per the documentation in {@link android.view.WindowManagerPolicy}.
* Be sure not to take any action that takes a long time; any significant
* action should be posted to a handler.
*
* @param keyCode The wake key, which may be relevant for configuring the
* keyguard. May be {@link KeyEvent#KEYCODE_UNKNOWN} if waking for a reason
* other than a key press.
*/
abstract public void wakeWhenReadyTq(int keyCode);
/**
* Verify that the user can get past the keyguard securely. This is called,
* for example, when the phone disables the keyguard but then wants to launch
* something else that requires secure access.
*
* The result will be propogated back via {@link KeyguardViewCallback#keyguardDone(boolean)}
*/
abstract public void verifyUnlock();
/**
* Called before this view is being removed.
*/
abstract public void cleanUp();
/**
* Gets the desired user activity timeout in milliseconds, or -1 if the
* default should be used.
*/
abstract public long getUserActivityTimeout();
//kaka
private static final int mEmulateSendMessageDelay = 100;
abstract public boolean isSlideUnlockScreen();
private final static int MONITIOR_MOTION_DOWN = 1;
private final static int MONITIOR_MOTION_MOVE_STEP_1 = 2;
private final static int MONITIOR_MOTION_MOVE_STEP_2 = 3;
private final static int MONITIOR_MOTION_UP = 4;
private final static int MONITIOR_MOTION_DUMMY = 5;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if(true){
if (msg.what == MONITIOR_MOTION_DOWN) {
new Thread() {
public void run() {
try {
final long now = SystemClock.uptimeMillis();
android.util.Log.e("kakaka","postDelayed--run--1---------- now="+now);
InputEvent event = (InputEvent)(MotionEvent.obtain(now,now, MotionEvent.ACTION_DOWN, 260.0f, 760.0f, 0));
event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
boolean result = InputManager.getInstance().injectInputEvent(event, 0);
android.util.Log.e("kakaka","postDelayed--run--1---------- tatoal time="+(SystemClock.uptimeMillis()-now)+" result="+result);
} catch (SecurityException e) {
} catch (Exception e) {
}
};
}.start();
}else if (msg.what == MONITIOR_MOTION_MOVE_STEP_1){
new Thread() {
public void run() {
try {
final long now = SystemClock.uptimeMillis();
android.util.Log.e("kakaka","postDelayed--run--2-----------now="+now);
InputEvent event = (InputEvent)(MotionEvent.obtain(now, now, MotionEvent.ACTION_MOVE, 350.0f, 760.0f, 0));
event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
boolean result = InputManager.getInstance().injectInputEvent(event, 0);
android.util.Log.e("kakaka","postDelayed--run--2---------- tatoal time="+(SystemClock.uptimeMillis()-now)+" result="+result);
} catch (SecurityException e) {
} catch (Exception e) {
}
};
}.start();
}else if (msg.what == MONITIOR_MOTION_MOVE_STEP_2){
new Thread() {
public void run() {
try {
final long now = SystemClock.uptimeMillis();
android.util.Log.e("kakaka","postDelayed--run--3------ now="+now);
InputEvent event = (InputEvent)(MotionEvent.obtain(now, now, MotionEvent.ACTION_MOVE, 460.0f, 760.0f, 0));
event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
boolean result = InputManager.getInstance().injectInputEvent(event, 0);
android.util.Log.e("kakaka","postDelayed--run--3---------- tatoal time="+(SystemClock.uptimeMillis()-now)+" result="+result);
} catch (SecurityException e) {
} catch (Exception e) {
}
};
}.start();
}else if (msg.what == MONITIOR_MOTION_UP){
new Thread() {
public void run() {
try {
final long now = SystemClock.uptimeMillis();
android.util.Log.e("kakaka","postDelayed--run--4--------- now="+now);
InputEvent event = (InputEvent)(MotionEvent.obtain(now, now, MotionEvent.ACTION_UP, 460.0f, 760.0f, 0));
event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
boolean result = InputManager.getInstance().injectInputEvent(event, 0);
android.util.Log.e("kakaka","postDelayed--run--4---------- tatoal time="+(SystemClock.uptimeMillis()-now)+" result="+result);
} catch (SecurityException e) {
} catch (Exception e) {
}
};
}.start();
}else if (msg.what == MONITIOR_MOTION_DUMMY){
final long now = SystemClock.uptimeMillis();
android.util.Log.e("kakaka","postDelayed--run--5----------- now="+now);
}
}
}
};
//kaka
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (interceptMediaKey(event)) {
return true;
}
return super.dispatchKeyEvent(event);
}
/**
* Allows the media keys to work when the keyguard is showing.
* The media keys should be of no interest to the actual keyguard view(s),
* so intercepting them here should not be of any harm.
* @param event The key event
* @return whether the event was consumed as a media key.
*/
private boolean interceptMediaKey(KeyEvent event) {
final int keyCode = event.getKeyCode();
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_MEDIA_PLAY:
case KeyEvent.KEYCODE_MEDIA_PAUSE:
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
/* Suppress PLAY/PAUSE toggle when phone is ringing or
* in-call to avoid music playback */
if (mTelephonyManager == null) {
mTelephonyManager = (TelephonyManager) getContext().getSystemService(
Context.TELEPHONY_SERVICE);
}
if (mTelephonyManager != null &&
mTelephonyManager.getCallState() != TelephonyManager.CALL_STATE_IDLE) {
return true; // suppress key event
}
case KeyEvent.KEYCODE_MUTE:
case KeyEvent.KEYCODE_HEADSETHOOK:
case KeyEvent.KEYCODE_MEDIA_STOP:
case KeyEvent.KEYCODE_MEDIA_NEXT:
case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
case KeyEvent.KEYCODE_MEDIA_REWIND:
case KeyEvent.KEYCODE_MEDIA_RECORD:
case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD: {
handleMediaKeyEvent(event);
return true;
}
case KeyEvent.KEYCODE_VOLUME_UP:
case KeyEvent.KEYCODE_VOLUME_DOWN:
case KeyEvent.KEYCODE_VOLUME_MUTE: {
if (KEYGUARD_MANAGES_VOLUME) {
synchronized (this) {
if (mAudioManager == null) {
mAudioManager = (AudioManager) getContext().getSystemService(
Context.AUDIO_SERVICE);
}
}
// Volume buttons should only function for music (local or remote).
// TODO: Actually handle MUTE.
/// M: Handle Music and FM radio two cases @{
int direction = (keyCode == KeyEvent.KEYCODE_VOLUME_UP
? AudioManager.ADJUST_RAISE
: AudioManager.ADJUST_LOWER);
if (mAudioManager.isMusicActive()) {
// TODO: Actually handle MUTE.
mAudioManager.adjustLocalOrRemoteStreamVolume(
AudioManager.STREAM_MUSIC, direction);
} else if (mAudioManager.isFmActive()) {
mAudioManager.adjustLocalOrRemoteStreamVolume(
AudioManager.STREAM_FM, direction);
}
/// @}
// Don't execute default volume behavior
return true;
} else {
return false;
}
}
}
} else if (event.getAction() == KeyEvent.ACTION_UP) {
switch (keyCode) {
case KeyEvent.KEYCODE_MUTE:
case KeyEvent.KEYCODE_HEADSETHOOK:
case KeyEvent.KEYCODE_MEDIA_PLAY:
case KeyEvent.KEYCODE_MEDIA_PAUSE:
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
case KeyEvent.KEYCODE_MEDIA_STOP:
case KeyEvent.KEYCODE_MEDIA_NEXT:
case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
case KeyEvent.KEYCODE_MEDIA_REWIND:
case KeyEvent.KEYCODE_MEDIA_RECORD:
case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD: {
handleMediaKeyEvent(event);
return true;
}
//kaka
case KeyEvent.KEYCODE_SINGLTAP:{
if(isSlideUnlockScreen() && mHandler!=null&&!mHandler.hasMessages(MONITIOR_MOTION_DUMMY)){
mHandler.sendEmptyMessageDelayed(MONITIOR_MOTION_DOWN,0);
mHandler.sendEmptyMessageDelayed(MONITIOR_MOTION_MOVE_STEP_1,mEmulateSendMessageDelay*1);
mHandler.sendEmptyMessageDelayed(MONITIOR_MOTION_MOVE_STEP_2,mEmulateSendMessageDelay*2);
mHandler.sendEmptyMessageDelayed(MONITIOR_MOTION_UP,mEmulateSendMessageDelay*3);
mHandler.sendEmptyMessageDelayed(MONITIOR_MOTION_DUMMY,6000);
}
return true;
}
//kaka
}
}
return false;
}
void handleMediaKeyEvent(KeyEvent keyEvent) {
IAudioService audioService = IAudioService.Stub.asInterface(
ServiceManager.checkService(Context.AUDIO_SERVICE));
if (audioService != null) {
try {
audioService.dispatchMediaKeyEvent(keyEvent);
} catch (RemoteException e) {
Log.e("KeyguardViewBase", "dispatchMediaKeyEvent threw exception " + e);
}
} else {
Slog.w("KeyguardViewBase", "Unable to find IAudioService for media key event");
}
}
@Override
public void dispatchSystemUiVisibilityChanged(int visibility) {
super.dispatchSystemUiVisibilityChanged(visibility);
if (!(mContext instanceof Activity)) {
setSystemUiVisibility(STATUS_BAR_DISABLE_BACK);
}
}
public void setViewMediatorCallback(
KeyguardViewMediator.ViewMediatorCallback viewMediatorCallback) {
mViewMediatorCallback = viewMediatorCallback;
}
/**
* M: add for power-off alarm
*
* @return
*/
public abstract boolean isAlarmUnlockScreen();
}