-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Judge if device is in pocket. * Notify clients callbacks when pocked state changes. * Start listening when device becomes not interactive. * Stop listening when device becomes interactive and is NOT in pocket. * Addresses NOUGAT-9. Signed-off-by: klozz <[email protected]>
- Loading branch information
Showing
12 changed files
with
1,015 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* Copyright (C) 2016 The ParanoidAndroid Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package android.pocket; | ||
|
||
/** @hide */ | ||
interface IPocketCallback { | ||
|
||
// notify when pocket state changes. | ||
void onStateChanged(boolean isDeviceInPocket, int reason); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* Copyright (C) 2016 The ParanoidAndroid Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package android.pocket; | ||
|
||
import android.pocket.IPocketCallback; | ||
|
||
/** @hide */ | ||
interface IPocketService { | ||
|
||
// add callback to get notified about pocket state. | ||
void addCallback(IPocketCallback callback); | ||
|
||
// remove callback and stop getting notified about pocket state. | ||
void removeCallback(IPocketCallback callback); | ||
|
||
// notify pocket service about intercative state changed. | ||
// @see com.android.policy.PhoneWindowManager | ||
void onInteractiveChanged(boolean interactive); | ||
|
||
// external processes can request changing listening state. | ||
void setListeningExternal(boolean listen); | ||
|
||
// check if device is in pocket. | ||
boolean isDeviceInPocket(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package android.pocket; | ||
|
||
/** | ||
* This class contains global pocket setup constants. | ||
* @author Carlo Savignano | ||
* @hide | ||
*/ | ||
|
||
public class PocketConstants { | ||
|
||
public static final boolean DEBUG = false; | ||
public static final boolean DEBUG_SPEW = false; | ||
|
||
/** | ||
* Whether to use proximity sensor to evaluate pocket state. | ||
*/ | ||
public static final boolean ENABLE_PROXIMITY_JUDGE = true; | ||
|
||
/** | ||
* Whether to use light sensor to evaluate pocket state. | ||
*/ | ||
public static final boolean ENABLE_LIGHT_JUDGE = true; | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
/** | ||
* Copyright (C) 2016 The ParanoidAndroid Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package android.pocket; | ||
|
||
import android.content.Context; | ||
import android.os.RemoteException; | ||
import android.util.Log; | ||
import android.util.Slog; | ||
|
||
/** | ||
* A class that coordinates listening for pocket state. | ||
* <p> | ||
* Use {@link android.content.Context#getSystemService(java.lang.String)} | ||
* with argument {@link android.content.Context#POCKET_SERVICE} to get | ||
* an instance of this class. | ||
* | ||
* Usage: import and create a final {@link IPocketCallback.Stub()} and implement your logic in | ||
* {@link IPocketCallback#onStateChanged(boolean, int)}. Then add your callback to the pocket manager | ||
* | ||
* // define a final callback | ||
* private final IPocketCallback mCallback = new IPocketCallback.Stub() { | ||
* | ||
* @Override | ||
* public void onStateChanged(boolean isDeviceInPocket, int reason) { | ||
* // Your method to handle logic outside of this callback, ideally with a handler | ||
* // posting on UI Thread for view hierarchy operations or with its own background thread. | ||
* handlePocketStateChanged(isDeviceInPocket, reason); | ||
* } | ||
* | ||
* } | ||
* | ||
* // add callback to pocket manager | ||
* private void addCallback() { | ||
* PocketManager manager = (PocketManager) context.getSystemService(Context.POCKET_SERVICE); | ||
* manager.addCallback(mCallback); | ||
* } | ||
* | ||
* @author Carlo Savignano | ||
* @hide | ||
*/ | ||
public class PocketManager { | ||
|
||
private static final String TAG = PocketManager.class.getSimpleName(); | ||
|
||
/** | ||
* Whether {@link IPocketCallback#onStateChanged(boolean, int)} | ||
* was fired because of the sensor. | ||
* @see PocketService#handleDispatchCallbacks() | ||
*/ | ||
public static final int REASON_SENSOR = 0; | ||
|
||
/** | ||
* Whether {@link IPocketCallback#onStateChanged(boolean, int)} | ||
* was fired because of an error while accessing service. | ||
* @see #addCallback(IPocketCallback) | ||
* @see #removeCallback(IPocketCallback) | ||
*/ | ||
public static final int REASON_ERROR = 1; | ||
|
||
/** | ||
* Whether {@link IPocketCallback#onStateChanged(boolean, int)} | ||
* was fired because of a needed reset. | ||
* @see PocketService#binderDied() | ||
*/ | ||
public static final int REASON_RESET = 2; | ||
|
||
private Context mContext; | ||
private IPocketService mService; | ||
|
||
public PocketManager(Context context, IPocketService service) { | ||
mContext = context; | ||
mService = service; | ||
if (mService == null) { | ||
Slog.v(TAG, "PocketService was null"); | ||
} | ||
} | ||
|
||
/** | ||
* Add pocket state callback. | ||
* @see PocketService#handleRemoveCallback(IPocketCallback) | ||
*/ | ||
public void addCallback(final IPocketCallback callback) { | ||
if (mService != null) try { | ||
mService.addCallback(callback); | ||
} catch (RemoteException e1) { | ||
Log.w(TAG, "Remote exception in addCallback: ", e1); | ||
if (callback != null){ | ||
try { | ||
callback.onStateChanged(false, REASON_ERROR); | ||
} catch (RemoteException e2) { | ||
Log.w(TAG, "Remote exception in callback.onPocketStateChanged: ", e2); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Remove pocket state callback. | ||
* @see PocketService#handleAddCallback(IPocketCallback) | ||
*/ | ||
public void removeCallback(final IPocketCallback callback) { | ||
if (mService != null) try { | ||
mService.removeCallback(callback); | ||
} catch (RemoteException e1) { | ||
Log.w(TAG, "Remote exception in removeCallback: ", e1); | ||
if (callback != null){ | ||
try { | ||
callback.onStateChanged(false, REASON_ERROR); | ||
} catch (RemoteException e2) { | ||
Log.w(TAG, "Remote exception in callback.onPocketStateChanged: ", e2); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Notify service about device interactive state changed. | ||
* {@link PhoneWindowManager#startedWakingUp()} | ||
* {@link PhoneWindowManager#startedGoingToSleep(int)} | ||
*/ | ||
public void onInteractiveChanged(boolean interactive) { | ||
if (mService != null) try { | ||
mService.onInteractiveChanged(interactive); | ||
} catch (RemoteException e) { | ||
Log.w(TAG, "Remote exception in addCallback: ", e); | ||
} | ||
} | ||
|
||
/** | ||
* Request listening state change by, but not limited to, external process. | ||
* @see PocketService#handleSetListeningExternal(boolean) | ||
*/ | ||
public void setListeningExternal(boolean listen) { | ||
if (mService != null) try { | ||
mService.setListeningExternal(listen); | ||
} catch (RemoteException e) { | ||
Log.w(TAG, "Remote exception in setListeningExternal: ", e); | ||
} | ||
} | ||
|
||
/** | ||
* Return whether device is in pocket. | ||
* @see PocketService#isDeviceInPocket() | ||
* @return | ||
*/ | ||
public boolean isDeviceInPocket() { | ||
if (mService != null) try { | ||
return mService.isDeviceInPocket(); | ||
} catch (RemoteException e) { | ||
Log.w(TAG, "Remote exception in isDeviceInPocket: ", e); | ||
} | ||
return false; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.