package com.alfeye.a1io; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.preference.PreferenceManager; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * 设备相关操作 工具类 * Created by DELL on 2018/11/30. */ public class DeviceControl { private static final String ACTION_CAMERA_HDR = "com.alf.camera.hdr"; private static final String KEY_CAMERA_HDR = "key_camera_hdr"; private static final String DEV_INFORMATIONSCREEN = "com.alf.screen.action"; private static final String KEY_OPEN_SCREEN = "input keyevent 224"; private static final String KEY_CLOSE_SCREEN = "input keyevent 223"; private static SharedPreferences sharedPreferences; //------------------------------------------------------- /** * 安装 本地应用 */ private static final String ACTION_INSTALL_PACKAGES = "com.alfeye.upgradeserver.action_install_packages"; /** * APK包名 */ private static String KEY_PACKAGENAME = "key_packagename"; /** * Apk路径 */ private static String KEY_APK_FILEPATH = "key_apk_filepath"; /** * 是否显示安装界面 */ private static String KEY_IS_SHOW_LOADING = "key_is_show_loading"; /** * 安装完成 是否启动APK */ private static String KEY_IS_START_APK = "key_is_start_apk"; private DeviceControl() { } /** * 开启屏幕并预览 * * **/ public static void openScreen() { execCommandSilent(KEY_OPEN_SCREEN); } /** * 关闭屏幕 需要系統簽名 **/ public static void closeScreen() { execCommandSilent(KEY_CLOSE_SCREEN); } /** * 关闭 * * @param command */ public static void execCommandSilent(String command) { try { Process p = Runtime.getRuntime().exec(new String[]{"/system/bin/sh", "-c", command}); BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); String readLine = br.readLine(); while (readLine != null) { readLine = br.readLine(); } br.close(); p.destroy(); } catch (IOException e) { e.printStackTrace(); } } /** * 初始化 * * @param context */ private static void init(Context context) { if (sharedPreferences == null) { sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); } } /** * 摄像头宽动态控制 * * @param context * @param state */ public static void configCameraHdr(Context context, boolean state) { if (sharedPreferences == null) { init(context); } sendCameraHdrBroadcast(context, state); sharedPreferences.edit().putBoolean(KEY_CAMERA_HDR, state).apply(); } /** * 获取Hdr的状态 */ public static boolean getCameraHdrStatus(Context context) { if (sharedPreferences == null) { init(context); } return sharedPreferences.getBoolean(KEY_CAMERA_HDR, false); } /** * A1屏幕息屏 亮屏 * 不需要簽名 * @param context * @param state true亮屏 false息屏 */ public static void sendInformationScreen(Context context, boolean state) { Intent camerIntent = new Intent(DEV_INFORMATIONSCREEN); camerIntent.putExtra("on", state); context.sendBroadcast(camerIntent); } /** * 摄像头宽动态控制 * * @param context * @param state true开启 false关闭 */ private static void sendCameraHdrBroadcast(Context context, boolean state) { Intent camerIntent = new Intent(ACTION_CAMERA_HDR); camerIntent.putExtra("enabled", state ? "true" : "false"); context.sendBroadcast(camerIntent); } /** * 设备重启 * * @param context */ public static void rebootSystem(Context context) { Intent devIntent = new Intent(); devIntent.setAction("com.alf.reboot.system"); context.sendBroadcast(devIntent); } /** * 隐藏 系统导航栏 * * @param context * @param state true 隐藏 false 还原显示 */ public static void hideNavigationBar(Context context, boolean state) { Intent sbarIntent = new Intent(); sbarIntent.setAction("com.alf.switch_status_bar"); sbarIntent.putExtra("hide", state); context.sendBroadcast(sbarIntent); } /** * 设置应用为 系统launcher * 需要再androidManifest添加 * 启动的activity 需要设置为 HOME LAUNCHER * * * * * * * * @param context * @param pkgname 包名 */ public static void setSystemLauncher(Context context, String pkgname) { Intent intentLauncher = new Intent(); //设置launcher 广播 intentLauncher.setAction("com.alf.set.default.launcher"); intentLauncher.putExtra("pkgname", pkgname); context.sendBroadcast(intentLauncher); } /** * 静默安装 APK * * @param context * @param apkPackageName APK包名 * @param apkFilePath Apk路径 * @param isShowLoading 是否显示安装加载界面 * @param isStartApk 安装完成 是否启动APK */ public static void startInstallApk(Context context, String apkPackageName, String apkFilePath, boolean isShowLoading, boolean isStartApk) { try { Intent intent = new Intent(); intent.setPackage("com.alfeye.upgradeserver"); intent.setAction(ACTION_INSTALL_PACKAGES); intent.putExtra(KEY_PACKAGENAME, apkPackageName); intent.putExtra(KEY_APK_FILEPATH, apkFilePath); intent.putExtra(KEY_IS_SHOW_LOADING, isShowLoading); intent.putExtra(KEY_IS_START_APK, isStartApk); context.startService(intent); } catch (Exception e) { e.printStackTrace(); } } }