目的
通过代码模拟光传感器的实现
主要思路
接通电话或者拔打电话的时候,五秒后屏灭,按键后屏亮!
代码实现
+++ b/alps/packages/apps/Phone/src/com/android/phone/InCallScreen.java
@@ -828,6 +828,12 @@ public class InCallScreen extends Activity
mApp = PhoneApp.getInstance();
mApp.setInCallScreenInstance(this);
+
+ boolean enable_lockindail = getResources().getBoolean(R.bool.enable_lockindail);
+ if(lockReceiver == null && enable_lockindail == true){
+ lockReceiver = new CloseLockActivityReceiver();
+ registerReceiver(lockReceiver, getLockIntentFilter());
+ }
// Make sure this is a voice-capable device.
if (!PhoneApp.sVoiceCapable) {
@@ -1375,6 +1381,15 @@ public class InCallScreen extends Activity
protected void onStop() {
if (DBG) log("onStop()...");
super.onStop();
+
+ Intent lockintent = new Intent(lockAction);
+ Bundle bundle = new Bundle();
+ bundle.putString("lock", "stop");
+ lockintent.putExtras(bundle);
+ sendBroadcast(lockintent);
+ if(LockinDailerActivity._instance != null){
+ LockinDailerActivity._instance.finish();
+ }
getWindow().getDecorView().getViewTreeObserver().removeOnPostDrawListener(mPostDrawListener);
@@ -1420,6 +1435,19 @@ public class InCallScreen extends Activity
unregisterReceiver(mLocaleChangeReceiver);
unregisterReceiver(mDMLockReceiver);
+ if(lockReceiver != null){
+ Intent lockintent = new Intent(lockAction);
+ Bundle bundle = new Bundle();
+ bundle.putString("lock", "stop");
+ lockintent.putExtras(bundle);
+ sendBroadcast(lockintent);
+ Log.i("aron","sendBroadcast stop");
+ if(LockinDailerActivity._instance != null)
+ LockinDailerActivity._instance.finish();
+ LOCK_TIME = 3;
+ unregisterReceiver(lockReceiver);
+ }
+
mApp.clearInCallScreenInstance(this);
// Clear out the InCallScreen references in various helper objects
@@ -2630,10 +2658,60 @@ public class InCallScreen extends Activity
}
updateLocalCache();
+ mForegroundLastState = mForegroundCall.getState();
+ mBackgroundLastState = mBackgroundCall.getState();
+ mRingingLastState = mRingingCall.getState();
+ //mLastInCallScreenMode = mInCallScreenMode;
if(DBG) log("fgCall state : " + mForegroundCall.getState());
if(DBG) log("bgCall state : " + mBackgroundCall.getState());
if(DBG) log("ringingCall state : " + mRingingCall.getState());
-
+ Log.i("zyc"," mForegroundLastState = " + mForegroundLastState
+ + " mBackgroundLastState = " + mBackgroundLastState
+ + " mRingingLastState = " + mRingingLastState
+// + " mLastInCallScreenMode = " + mLastInCallScreenMode
+ );
+ if(Call.State.ACTIVE == mForegroundLastState){
+ Intent lockintent = new Intent(lockAction);
+ Bundle bundle = new Bundle();
+ bundle.putString("lock", "start");
+ lockintent.putExtras(bundle);
+ sendBroadcast(lockintent);
+ Log.i("zyc","ACTIVE sendBroadcast start");
+ }else if(Call.State.DISCONNECTED == mForegroundLastState){
+ Intent lockintent = new Intent(lockAction);
+ Bundle bundle = new Bundle();
+ bundle.putString("lock", "stop");
+ lockintent.putExtras(bundle);
+ sendBroadcast(lockintent);
+ Log.i("zyc","DISCONNECTED sendBroadcast stop");
+ LOCK_TIME = 3;
+ if(LockinDailerActivity._instance != null){
+ LockinDailerActivity._instance.finish();
+ }
+ }else if(Call.State.DISCONNECTING == mForegroundLastState){
+ Intent lockintent = new Intent(lockAction);
+ Bundle bundle = new Bundle();
+ bundle.putString("lock", "stop");
+ lockintent.putExtras(bundle);
+ sendBroadcast(lockintent);
+ Log.i("zyc","DISCONNECTING sendBroadcast stop");
+ LOCK_TIME = 3;
+ if(LockinDailerActivity._instance != null){
+ LockinDailerActivity._instance.finish();
+ }
+ } else if(mApp.inCallUiState.inCallScreenMode == InCallScreenMode.CALL_ENDED){
+ Intent lockintent = new Intent(lockAction);
+ Bundle bundle = new Bundle();
+ bundle.putString("lock", "stop");
+ lockintent.putExtras(bundle);
+ sendBroadcast(lockintent);
+ Log.i("zyc","CALL_ENDED sendBroadcast stop");
+ LOCK_TIME = 3;
+ if(LockinDailerActivity._instance != null){
+ LockinDailerActivity._instance.finish();
+ }
+ }
+
// Update the onscreen UI.
// We use requestUpdateScreen() here (which posts a handler message)
// instead of calling updateScreen() directly, which allows us to avoid
@@ -4008,6 +4086,12 @@ public class InCallScreen extends Activity
// Also, any time we hold or unhold, force the DTMF dialpad to close.
hideDialpadInternal(true); // do the "closing" animation
+ Intent lockintent = new Intent("com.android.phone.lockindail");
+ Bundle bundle = new Bundle();
+ bundle.putString("lock", "reset");
+ lockintent.putExtras(bundle);
+ getApplicationContext().sendBroadcast(lockintent);
+ Log.i("zyc","onHoldClick sendBroadcast reset");
}
/**
@@ -4038,6 +4122,12 @@ public class InCallScreen extends Activity
if (VTCallUtils.VTScreenMode.VT_SCREEN_OPEN == mVTInCallScreen.getVTScreenMode())
mVTInCallScreen.updateVTScreen(mVTInCallScreen.getVTScreenMode());
}
+ Intent lockintent = new Intent("com.android.phone.lockindail");
+ Bundle bundle = new Bundle();
+ bundle.putString("lock", "reset");
+ lockintent.putExtras(bundle);
+ getApplicationContext().sendBroadcast(lockintent);
+ Log.i("zyc","toggleSpeaker sendBroadcast reset");
}
/*
@@ -4051,6 +4141,12 @@ public class InCallScreen extends Activity
if (VTCallUtils.VTScreenMode.VT_SCREEN_OPEN == mVTInCallScreen.getVTScreenMode())
mVTInCallScreen.updateVTScreen(mVTInCallScreen.getVTScreenMode());
}
+ Intent lockintent = new Intent("com.android.phone.lockindail");
+ Bundle bundle = new Bundle();
+ bundle.putString("lock", "reset");
+ lockintent.putExtras(bundle);
+ getApplicationContext().sendBroadcast(lockintent);
+ Log.i("zyc","onMuteClick sendBroadcast reset");
}
/*private*/ void onSpeakerClick() {
@@ -6933,4 +7029,163 @@ public class InCallScreen extends Activity
Process.setThreadPriority(myId, Process.THREAD_PRIORITY_DEFAULT);
}
}
+
+ private LockinDailer mLockinDailer = null;
+ private IntentFilter lockIntentFilter = null;
+ private BroadcastReceiver lockReceiver = null;
+ private String lockAction = "com.android.phone.lockindail";
+ private int LOCK_TIME = 3;
+ private class LockinDailer{
+ private final int MESSAGE_ID = 1;
+ private int lock_count = 0;
+
+ lockHander mlockHandler;
+ lockThread mlockThread;
+
+ public LockinDailer() {
+ Log.i(LOG_TAG, "LockinDailer->construct");
+ lock_count = 0;
+ mlockHandler = new lockHander();
+ mlockThread = new lockThread();
+
+ }
+
+ public void start_lockThread(){
+ Log.i(LOG_TAG, "LockinDailer->star_lockThread");
+ mlockThread.start();
+ }
+
+ public void interrupted_lockThread(){
+ Log.i(LOG_TAG, "LockinDailer->interrupted_lockThread");
+ if(mlockThread.isAlive())
+ mlockThread.interrupt();
+
+ }
+
+ public void reset_lock_time(){
+ Log.i(LOG_TAG, "LockinDailer->reset_lock_time,lock_count= "+lock_count);
+ lock_count = 0;
+ }
+
+ private class lockHander extends Handler{
+ // @Override
+ public void handleMessage(Message msg)
+ {
+ switch (msg.what)
+ {
+ case MESSAGE_ID:
+ if(lock_count >= LOCK_TIME)
+ {
+ reset_lock_time();
+ interrupted_lockThread();
+ Log.i(LOG_TAG, "LockinDailer->startlockactivity,lock_count="+lock_count);
+
+ Intent lockintent = new Intent(lockAction);
+ Bundle bundle = new Bundle();
+ bundle.putString("lock", "stop");
+ lockintent.putExtras(bundle);
+ sendBroadcast(lockintent);
+ if(LOCK_TIME != 5){
+ LOCK_TIME = 5;
+ }
+ Intent mIntent = new Intent(InCallScreen.this, LockinDailerActivity.class);
+ startActivity(mIntent);
+ }
+ else
+ {
+ Log.i(LOG_TAG, "handleMessage,lock_count="+lock_count);
+ lock_count++;
+ }
+ break;
+
+ }
+ }
+ }
+
+ private class lockThread extends Thread{
+
+ @Override
+ public void run() {
+ while(true)
+ {
+ try {
+ sleep(1000);
+ }
+ catch ( InterruptedException e) {
+ return;
+ }
+
+ Message msg = new Message();
+ msg.what = MESSAGE_ID;
+ mlockHandler.sendMessage(msg);
+ }
+
+ }
+
+ }
+ }
+
+ private class CloseLockActivityReceiver extends BroadcastReceiver{
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ String action = intent.getAction();
+ if (action.equals(lockAction)){
+ Bundle bundle = intent.getExtras();
+ String lockString = bundle.getString("lock");
+ Log.i("zyc","CloseLockActivityReceiver lock = " + lockString);
+ Phone.State state = mCM.getState();
+ if(lockString.equals("start")){
+ if(mLockinDailer == null){
+ mLockinDailer = new LockinDailer();
+ mLockinDailer.reset_lock_time();
+ mLockinDailer.start_lockThread();
+ //aeon panhongyu 20121203
+ if(mIsForegroundActivity == false || mVTInCallScreen.getVTScreenMode() == VTCallUtils.VTScreenMode.VT_SCREEN_OPEN){
+ //aeon panhongyu 20121203
+ Intent lockintent = new Intent(lockAction);
+ Bundle stopbundle = new Bundle();
+ stopbundle.putString("lock", "stop");
+ lockintent.putExtras(stopbundle);
+ sendBroadcast(lockintent);
+ Log.i("zyc","onReceive sendBroadcast stop");
+ }
+ }
+ }else if(lockString.equals("reset")){
+ if(mLockinDailer != null){
+ mLockinDailer.reset_lock_time();
+ }
+ }else if(lockString.equals("stop")){
+ if(mLockinDailer != null){
+ mLockinDailer.interrupted_lockThread();
+ mLockinDailer = null;
+ }
+ }
+ }else if(action.equals(Intent.ACTION_SCREEN_ON)){
+ Intent lockintent = new Intent(lockAction);
+ Bundle stopbundle = new Bundle();
+ stopbundle.putString("lock", "start");
+ lockintent.putExtras(stopbundle);
+ sendBroadcast(lockintent);
+ Log.i("zyc","ACTION_SCREEN_ON sendBroadcast start");
+ }else if(action.equals(Intent.ACTION_SCREEN_OFF)){
+ Intent lockintent = new Intent(lockAction);
+ Bundle stopbundle = new Bundle();
+ stopbundle.putString("lock", "stop");
+ lockintent.putExtras(stopbundle);
+ sendBroadcast(lockintent);
+ Log.i("zyc","ACTION_SCREEN_OFF sendBroadcast stop");
+ }
+ }
+ }
+
+ private IntentFilter getLockIntentFilter(){
+ if(lockIntentFilter == null){
+ lockIntentFilter = new IntentFilter();
+ lockIntentFilter.addAction(lockAction);
+ lockIntentFilter.addAction(Intent.ACTION_SCREEN_ON);
+ lockIntentFilter.addAction(Intent.ACTION_SCREEN_OFF);
+ }
+ return lockIntentFilter;
+ }
+
}
+++ b/alps/packages/apps/Phone/src/com/android/phone/LockinDailerActivity.java
+++ b/alps/packages/apps/Phone/AndroidManifest.xml
@@ -805,6 +805,15 @@
android:theme="@*android:style/Theme.Holo.Dialog.Alert">
</activity>
+
+ <activity android:name="LockinDailerActivity"
+ android:configChanges="orientation"
+ android:theme="@style/transparent">
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+ </intent-filter>
+ </activity>
+
<service android:name="PhoneRecorderServices"></service>
<provider android:name=".CallRejectContentProvider" android:authorities="reject" />
</application>
源码地址:http://download.csdn.net/detail/topicdev/5299360 点击打开链接