Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change-Id: I1349da4540424c678c554f242cb1030b6995c388 Signed-off-by: STELIX <ssspinni@gmail.com>
- Loading branch information
Showing
1 changed file
with
346 additions
and
0 deletions.
There are no files selected for viewing
346 changes: 346 additions & 0 deletions
346
core/java/com/android/internal/util/aokp/AOKPUtils.java
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,346 @@ | ||
/* | ||
* Copyright (C) 2017 AICP | ||
* Copyright (C) 2018 AOKP | ||
* | ||
* 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 com.android.internal.util.aokp; | ||
|
||
import android.app.ActivityManager; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.content.pm.ActivityInfo; | ||
import android.content.pm.ApplicationInfo; | ||
import android.content.pm.PackageManager; | ||
import android.content.pm.PackageManager.NameNotFoundException; | ||
import android.content.res.Resources; | ||
import android.graphics.Color; | ||
import android.hardware.camera2.CameraAccessException; | ||
import android.hardware.camera2.CameraCharacteristics; | ||
import android.hardware.camera2.CameraManager; | ||
import android.hardware.input.InputManager; | ||
import android.net.ConnectivityManager; | ||
import android.os.Handler; | ||
import android.os.Looper; | ||
import android.os.PowerManager; | ||
import android.os.UserHandle; | ||
import android.os.SystemClock; | ||
import android.util.Log; | ||
import android.view.InputDevice; | ||
import android.view.KeyCharacterMap; | ||
import android.view.KeyEvent; | ||
|
||
import java.util.List; | ||
|
||
import java.util.Locale; | ||
|
||
public class AOKPUtils { | ||
|
||
private static final String TAG = "AOKPUtils"; | ||
|
||
public static boolean isChineseLanguage() { | ||
return Resources.getSystem().getConfiguration().locale.getLanguage().startsWith( | ||
Locale.CHINESE.getLanguage()); | ||
} | ||
|
||
public static boolean isWifiOnly(Context context) { | ||
ConnectivityManager cm = (ConnectivityManager)context.getSystemService( | ||
Context.CONNECTIVITY_SERVICE); | ||
return (cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE) == false); | ||
} | ||
|
||
/** | ||
* Checks if a specific package is installed. | ||
* | ||
* @param context The context to retrieve the package manager | ||
* @param packageName The name of the package | ||
* @return Whether the package is installed or not. | ||
*/ | ||
public static boolean isPackageInstalled(Context context, String packageName) { | ||
PackageManager pm = context.getPackageManager(); | ||
try { | ||
if (pm != null) { | ||
List<ApplicationInfo> packages = pm.getInstalledApplications(0); | ||
for (ApplicationInfo packageInfo : packages) { | ||
if (packageInfo.packageName.equals(packageName)) { | ||
return true; | ||
} | ||
} | ||
} | ||
} catch (Exception e) { | ||
Log.e(TAG, "Error: " + e.getMessage()); | ||
} | ||
return false; | ||
} | ||
|
||
public static boolean isPackageEnabled(String packageName, PackageManager pm) { | ||
try { | ||
ApplicationInfo ai = pm.getApplicationInfo(packageName, 0); | ||
return ai.enabled; | ||
} catch (PackageManager.NameNotFoundException notFound) { | ||
return false; | ||
} | ||
} | ||
|
||
public static boolean isAvailableApp(String packageName, Context context) { | ||
Context mContext = context; | ||
final PackageManager pm = mContext.getPackageManager(); | ||
try { | ||
pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES); | ||
int enabled = pm.getApplicationEnabledSetting(packageName); | ||
return enabled != PackageManager.COMPONENT_ENABLED_STATE_DISABLED && | ||
enabled != PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER; | ||
} catch (NameNotFoundException e) { | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* Checks if a specific service is running. | ||
* | ||
* @param context The context to retrieve the activity manager | ||
* @param serviceName The name of the service | ||
* @return Whether the service is running or not | ||
*/ | ||
public static boolean isServiceRunning(Context context, String serviceName) { | ||
ActivityManager activityManager = (ActivityManager) context | ||
.getSystemService(Context.ACTIVITY_SERVICE); | ||
List<ActivityManager.RunningServiceInfo> services = activityManager | ||
.getRunningServices(Integer.MAX_VALUE); | ||
|
||
if (services != null) { | ||
for (ActivityManager.RunningServiceInfo info : services) { | ||
if (info.service != null) { | ||
if (info.service.getClassName() != null && info.service.getClassName() | ||
.equalsIgnoreCase(serviceName)) { | ||
return true; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Check if system has a camera. | ||
* | ||
* @param context | ||
* @return | ||
*/ | ||
public static boolean hasCamera(final Context context) { | ||
final PackageManager pm = context.getPackageManager(); | ||
return pm != null && pm.hasSystemFeature(PackageManager.FEATURE_CAMERA); | ||
} | ||
|
||
/** | ||
* Check if system has a front camera. | ||
* | ||
* @param context | ||
* @return | ||
*/ | ||
public static boolean hasFrontCamera(final Context context) { | ||
final PackageManager pm = context.getPackageManager(); | ||
return pm != null && pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT); | ||
} | ||
|
||
public static boolean deviceSupportsFlashLight(Context context) { | ||
CameraManager cameraManager = (CameraManager) context.getSystemService( | ||
Context.CAMERA_SERVICE); | ||
try { | ||
String[] ids = cameraManager.getCameraIdList(); | ||
for (String id : ids) { | ||
CameraCharacteristics c = cameraManager.getCameraCharacteristics(id); | ||
Boolean flashAvailable = c.get(CameraCharacteristics.FLASH_INFO_AVAILABLE); | ||
Integer lensFacing = c.get(CameraCharacteristics.LENS_FACING); | ||
if (flashAvailable != null | ||
&& flashAvailable | ||
&& lensFacing != null | ||
&& lensFacing == CameraCharacteristics.LENS_FACING_BACK) { | ||
return true; | ||
} | ||
} | ||
} catch (CameraAccessException e) { | ||
// Ignore | ||
} | ||
return false; | ||
} | ||
|
||
// Omni Switch Constants | ||
|
||
/** | ||
* Package name of the omnniswitch app | ||
*/ | ||
public static final String APP_PACKAGE_NAME = "org.omnirom.omniswitch"; | ||
|
||
/** | ||
* Intent broadcast action for toogle the omniswitch overlay | ||
*/ | ||
private static final String ACTION_TOGGLE_OVERLAY2 = APP_PACKAGE_NAME + ".ACTION_TOGGLE_OVERLAY2"; | ||
|
||
/** | ||
* Intent broadcast action for telling omniswitch to preload tasks | ||
*/ | ||
private static final String ACTION_PRELOAD_TASKS = APP_PACKAGE_NAME + ".ACTION_PRELOAD_TASKS"; | ||
|
||
/** | ||
* Intent broadcast action for restoring the home stack | ||
*/ | ||
private static final String ACTION_RESTORE_HOME_STACK = APP_PACKAGE_NAME + ".ACTION_RESTORE_HOME_STACK"; | ||
|
||
/** | ||
* Intent broadcast action for hide the omniswitch overlay | ||
*/ | ||
private static final String ACTION_HIDE_OVERLAY = APP_PACKAGE_NAME + ".ACTION_HIDE_OVERLAY"; | ||
|
||
/** | ||
* Intent for launching the omniswitch settings actvity | ||
*/ | ||
public static Intent INTENT_LAUNCH_APP = new Intent(Intent.ACTION_MAIN) | ||
.setClassName(APP_PACKAGE_NAME, APP_PACKAGE_NAME + ".SettingsActivity"); | ||
|
||
public static boolean isBillingBypassInstalled(final Context context) { | ||
boolean mBillingBypassInstalled = false; | ||
final String[] billingBypassPackageNames = { | ||
"com.dimonvideo.luckypatcher", // used by Lucky Patcher | ||
"com.chelpus.lackypatch", // used by Lucky Patcher | ||
"com.android.vending.billing.InAppBillingService.LACK", // used by Lucky Patcher | ||
"com.android.vending.billing.InAppBillingService.LOCK", // used by Lucky Patcher | ||
"com.android.vending.billing.InAppBillingService.CLON", // used by Lucky Patcher | ||
"com.android.vendinc", // used by Uret | ||
"uret.jasi2169.patcher" // used by Uret | ||
}; | ||
for (String billingBypassPackageName : billingBypassPackageNames) { | ||
mBillingBypassInstalled = mBillingBypassInstalled || isPackageInstalled(context, billingBypassPackageName); | ||
} | ||
|
||
return mBillingBypassInstalled; | ||
} | ||
|
||
/** | ||
* @hide | ||
*/ | ||
public static void toggleOmniSwitchRecents(Context context, UserHandle user) { | ||
final Intent intent = new Intent(AOKPUtils.ACTION_TOGGLE_OVERLAY2); | ||
intent.setPackage(APP_PACKAGE_NAME); | ||
context.sendBroadcastAsUser(intent, user); | ||
} | ||
|
||
/** | ||
* @hide | ||
*/ | ||
public static void hideOmniSwitchRecents(Context context, UserHandle user) { | ||
final Intent intent = new Intent(AOKPUtils.ACTION_HIDE_OVERLAY); | ||
intent.setPackage(APP_PACKAGE_NAME); | ||
context.sendBroadcastAsUser(intent, user); | ||
} | ||
|
||
/** | ||
* @hide | ||
*/ | ||
public static void restoreHomeStack(Context context, UserHandle user) { | ||
final Intent intent = new Intent(AOKPUtils.ACTION_RESTORE_HOME_STACK); | ||
intent.setPackage(APP_PACKAGE_NAME); | ||
context.sendBroadcastAsUser(intent, user); | ||
} | ||
|
||
/** | ||
* @hide | ||
*/ | ||
public static void preloadOmniSwitchRecents(Context context, UserHandle user) { | ||
final Intent intent = new Intent(AOKPUtils.ACTION_PRELOAD_TASKS); | ||
intent.setPackage(APP_PACKAGE_NAME); | ||
context.sendBroadcastAsUser(intent, user); | ||
} | ||
|
||
public static int getBlendColorForPercent(int fullColor, int emptyColor, boolean reversed, | ||
int percentage) { | ||
float[] newColor = new float[3]; | ||
float[] empty = new float[3]; | ||
float[] full = new float[3]; | ||
Color.colorToHSV(fullColor, full); | ||
int fullAlpha = Color.alpha(fullColor); | ||
Color.colorToHSV(emptyColor, empty); | ||
int emptyAlpha = Color.alpha(emptyColor); | ||
float blendFactor = percentage/100f; | ||
if (reversed) { | ||
if (empty[0] < full[0]) { | ||
empty[0] += 360f; | ||
} | ||
newColor[0] = empty[0] - (empty[0]-full[0])*blendFactor; | ||
} else { | ||
if (empty[0] > full[0]) { | ||
full[0] += 360f; | ||
} | ||
newColor[0] = empty[0] + (full[0]-empty[0])*blendFactor; | ||
} | ||
if (newColor[0] > 360f) { | ||
newColor[0] -= 360f; | ||
} else if (newColor[0] < 0) { | ||
newColor[0] += 360f; | ||
} | ||
newColor[1] = empty[1] + ((full[1]-empty[1])*blendFactor); | ||
newColor[2] = empty[2] + ((full[2]-empty[2])*blendFactor); | ||
int newAlpha = (int) (emptyAlpha + ((fullAlpha-emptyAlpha)*blendFactor)); | ||
return Color.HSVToColor(newAlpha, newColor); | ||
} | ||
|
||
public static void switchScreenOff(Context ctx) { | ||
PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE); | ||
if (pm!= null) { | ||
pm.goToSleep(SystemClock.uptimeMillis()); | ||
} | ||
} | ||
|
||
public static void sendKeycode(int keycode) { | ||
long when = SystemClock.uptimeMillis(); | ||
final KeyEvent evDown = new KeyEvent(when, when, KeyEvent.ACTION_DOWN, keycode, 0, | ||
0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, | ||
KeyEvent.FLAG_FROM_SYSTEM | KeyEvent.FLAG_VIRTUAL_HARD_KEY, | ||
InputDevice.SOURCE_KEYBOARD); | ||
final KeyEvent evUp = KeyEvent.changeAction(evDown, KeyEvent.ACTION_UP); | ||
|
||
final Handler handler = new Handler(Looper.getMainLooper()); | ||
handler.post(new Runnable() { | ||
@Override | ||
public void run() { | ||
InputManager.getInstance().injectInputEvent(evDown, | ||
InputManager.INJECT_INPUT_EVENT_MODE_ASYNC); | ||
} | ||
}); | ||
handler.postDelayed(new Runnable() { | ||
@Override | ||
public void run() { | ||
InputManager.getInstance().injectInputEvent(evUp, | ||
InputManager.INJECT_INPUT_EVENT_MODE_ASYNC); | ||
} | ||
}, 20); | ||
} | ||
|
||
public static ActivityInfo getRunningActivityInfo(Context context) { | ||
final ActivityManager am = (ActivityManager) context | ||
.getSystemService(Context.ACTIVITY_SERVICE); | ||
final PackageManager pm = context.getPackageManager(); | ||
|
||
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1); | ||
if (tasks != null && !tasks.isEmpty()) { | ||
ActivityManager.RunningTaskInfo top = tasks.get(0); | ||
try { | ||
return pm.getActivityInfo(top.topActivity, 0); | ||
} catch (PackageManager.NameNotFoundException e) { | ||
} | ||
} | ||
return null; | ||
} | ||
} |