From eadd9db01b24ccde96a129dafa989d4ec436cdfd Mon Sep 17 00:00:00 2001 From: ronnie <ronnie@industiosoft.com> Date: Sun, 23 Oct 2022 04:44:46 +0000 Subject: [PATCH] add ido services,delete boot on window --- android/packages/apps/Settings/src/com/android/settings/FallbackHome.java | 8 android/packages/apps/Settings/src/com/android/settings/InstallResultReceiver.java | 48 ++ android/packages/apps/Settings/src/com/android/settings/IdoDevCustomService.java | 965 ++++++++++++++++++++++++++++++++++++++++++++++++ android/packages/apps/Settings/src/com/android/settings/MyBootReceiver.java | 20 + android/packages/apps/Settings/src/com/android/settings/InstallUtils.java | 142 +++++++ 5 files changed, 1,179 insertions(+), 4 deletions(-) diff --git a/android/packages/apps/Settings/src/com/android/settings/FallbackHome.java b/android/packages/apps/Settings/src/com/android/settings/FallbackHome.java index e3944a6..e77d3e2 100755 --- a/android/packages/apps/Settings/src/com/android/settings/FallbackHome.java +++ b/android/packages/apps/Settings/src/com/android/settings/FallbackHome.java @@ -49,9 +49,9 @@ private WallpaperManager mWallManager; private final Runnable mProgressTimeoutRunnable = () -> { - View v = getLayoutInflater().inflate( - R.layout.fallback_home_finishing_boot, null /* root */); - setContentView(v); + //View v = getLayoutInflater().inflate( + // R.layout.fallback_home_finishing_boot, null /* root */); + /*setContentView(v); v.setAlpha(0f); v.animate() .alpha(1f) @@ -59,7 +59,7 @@ .setInterpolator(AnimationUtils.loadInterpolator( this, android.R.interpolator.fast_out_slow_in)) .start(); - getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON); + getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);*/ }; private final OnColorsChangedListener mColorsChangedListener = new OnColorsChangedListener() { diff --git a/android/packages/apps/Settings/src/com/android/settings/IdoDevCustomService.java b/android/packages/apps/Settings/src/com/android/settings/IdoDevCustomService.java new file mode 100755 index 0000000..92e6cc1 --- /dev/null +++ b/android/packages/apps/Settings/src/com/android/settings/IdoDevCustomService.java @@ -0,0 +1,965 @@ +package com.android.settings; + +import android.app.PendingIntent; +import android.app.Service; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.content.ComponentName; +import android.os.IBinder; +import android.os.PowerManager; +import android.os.SystemClock; +import android.util.Log; +import android.os.Handler; +import android.os.Environment; +import android.os.Binder; +import android.os.Build; +import android.os.ServiceManager; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.io.FileWriter; +import java.util.List; + +import android.os.SystemProperties; +import android.content.pm.PackageManager; +import android.content.pm.IPackageManager; +import android.content.pm.PackageInfo; +import android.content.pm.ApplicationInfo; +import android.text.TextUtils; +import android.provider.Settings; +import android.app.ActivityManager; +import android.net.Uri; +import android.app.PackageInstallObserver; +import android.content.pm.ResolveInfo; +import android.app.AlarmManager; + +import android.net.wifi.WifiConfiguration; +import android.net.wifi.WifiManager; +import android.net.wifi.WifiConfiguration.AuthAlgorithm; +import android.net.wifi.WifiConfiguration.KeyMgmt; + +import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT; + +import org.ido.iface.DeviceCtrl; + +import android.os.ServiceManager; +import android.os.Parcel; +import android.os.RemoteException; + +//import com.android.settings.development; + + +public class IdoDevCustomService extends Service { + + private static final String TAG = "IdoDevCustomService"; + + private static final String ACTION_APK_INSTALL = "android.intent.action.SILENT_INSTALL_PACKAGE"; + private static final String ACTION_TOUCH_TEST = "android.ido.intent.action.touchtest"; + private static final String ACTION_IDO_SET_REBOOT = "android.ido.intent.action.set.reboot"; + private static final String ACTION_IDO_SET_SHUTDOWN = "android.ido.intent.action.set.shutdown"; + private static final String ACTION_IDO_SET_FACTORY = "android.ido.intent.action.set.factory"; + private static final String ACTION_IDO_SET_AIRPLANE_MODE = "android.ido.intent.action.set.airPlaneMode"; + private static final String ACTION_IDO_SET_APP_CHECK = "android.ido.intent.action.set.appcheck"; + + private static final String ACTION_IDO_SCREEN_SHOT = "android.ido.intent.action.screenshot"; + private static final String ACTION_IDO_SET_TIME = "android.ido.intent.action.settime"; + private static final String ACTION_IDO_SET_WIFIAP = "android.ido.intent.action.wifiap"; + + private static final String ACTION_IDO_SET_SCREEN_BRIGHTNESS = "android.ido.action.screen.brightness"; + private static final String ACTION_IDO_SET_SCREEN = "android.ido.action.screen.set"; + private static final String ACTION_IDO_SET_SCREEN_ONOFF_GET = "android.ido.action.screenoff.get"; + private static final String ACTION_IDO_SET_TIMEOUT_SLEEP = "android.ido.action.sleep.set"; + private static final String ACTION_IDO_GET_TIMEOUT_SLEEP = "android.ido.action.sleep.get"; + private static final String ACTION_IDO_SET_LAUNCHER = "android.ido.intent.action.launcher"; + private static final String ACTION_IDO_GET_APK_INSTALLED = "android.ido.intent.action.apk.installed"; + private static final String ACTION_IDO_SET_ADB = "android.ido.intent.action.adb"; + private static final String ACTION_IDO_SET_INPUT = "android.ido.intent.action.reset.sleep"; + + private Context mContext; + + private static final boolean DEBUG = true; + + private static final int PACKAGE_NAME_START_INDEX = 8; + + private static final int CHECK_APP_LOOP_TIME = 2100;//ms + + private static final int TS_CHECK_LOOP_TIME1 = 1000;//ms + private static final int TS_CHECK_LOOP_TIME2 = 230;//ms + private static int NO_TOUCH_CNT_TIMEOUT = 600;//600;//10分钟 + + private static String installPackageName=""; + private static String installClassName=""; + + private boolean installedStartActivityFlag=false; + + private static boolean enableAppCheck=false; + private static String checkPackageName=""; + private static String runClassName=""; + private static int checkAppCnt=5;//检测APP是否在界面显示的次数,大于此次数还没有发现APP在前端运行则重启app + private static int checkAppErrCnt=0; + + private AlarmManager mAlarmManager = null; + + private WifiManager mWifiManager; + private WifiConfiguration mWifiConfig = null; + private int wifiApState = WifiManager.WIFI_AP_STATE_DISABLED; + private boolean restartWifiApFlag=false; + + private static int lastTsCnt,noTouchDataCnt,onTouchFlag=0; + + private IBinder mSurfaceFlinger; + + //private static int blBrightness; + + private DeviceCtrl mDevCtrl; + private static int ts_check_loop_time=TS_CHECK_LOOP_TIME1; + + + @Override + public IBinder onBind(Intent arg0) { + // TODO Auto-generated method stub + return null; + } + + @Override + public void onCreate() { + super.onCreate(); + + if(DEBUG) Log.d(TAG, "-----------------onCreate()"); + + mContext = this; + + IntentFilter mFilter = new IntentFilter(); + IntentFilter mFilter2 = new IntentFilter(); + + mFilter.addAction(ACTION_APK_INSTALL); + mFilter.addAction(ACTION_TOUCH_TEST); + mFilter.addAction(ACTION_IDO_SET_REBOOT); + mFilter.addAction(ACTION_IDO_SET_SHUTDOWN); + mFilter.addAction(ACTION_IDO_SET_FACTORY); + mFilter.addAction(ACTION_IDO_SET_AIRPLANE_MODE); + mFilter.addAction(ACTION_IDO_SET_APP_CHECK); + mFilter.addAction(ACTION_IDO_SCREEN_SHOT); + mFilter.addAction(ACTION_IDO_SET_TIME); + mFilter.addAction(ACTION_IDO_SET_WIFIAP); + mFilter.addAction(WifiManager.WIFI_AP_STATE_CHANGED_ACTION); + mFilter.addAction(ACTION_IDO_SET_SCREEN_BRIGHTNESS); + mFilter.addAction(ACTION_IDO_SET_SCREEN); + mFilter.addAction(ACTION_IDO_SET_SCREEN_ONOFF_GET); + mFilter.addAction(ACTION_IDO_SET_LAUNCHER); + mFilter.addAction(ACTION_IDO_SET_TIMEOUT_SLEEP); + mFilter.addAction(ACTION_IDO_GET_TIMEOUT_SLEEP); + mFilter.addAction(ACTION_IDO_SET_ADB); + mFilter.addAction(ACTION_IDO_SET_INPUT); + + registerReceiver(mReceiver, mFilter); + + mFilter2.addAction(Intent.ACTION_PACKAGE_ADDED); + //mFilter2.addAction(Intent.ACTION_PACKAGE_REPLACED);//替换安装也会有ADDED广播 + mFilter2.addDataScheme("package"); + registerReceiver(mReceiver2, mFilter2); + + try { + mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); + } catch (Exception e) { + Log.e(TAG, "Get AlarmManager error!\n"); + e.printStackTrace(); + } + + try { + mDevCtrl = new DeviceCtrl(); + //blBrightness = getSystemBrightness(); + } catch (Exception e) { + Log.e(TAG, "DeviceCtrl error!\n"); + e.printStackTrace(); + } + + try { + if ("true".equals(SystemProperties.get("persist.ido.appcheck.enable","false"))) { + + checkPackageName = SystemProperties.get("persist.ido.appcheck.packageName",""); + runClassName = SystemProperties.get("persist.ido.appcheck.className",""); + checkAppCnt = Integer.parseInt(SystemProperties.get("persist.ido.appcheck.checkCnt","3"));//unit:2s,//检测APP是否在界面显示的次数,大于此次数还没有发现APP在前端运行则重启app,默认3次 + + if (runClassName.equals("") == false && runClassName.equals("") == false) { + mHandler.postDelayed(appCheckRunnable, 2 * CHECK_APP_LOOP_TIME); + enableAppCheck = true; + } + } + + NO_TOUCH_CNT_TIMEOUT = Integer.parseInt(SystemProperties.get("persist.ido.screen.timeout","600")); + Log.d(TAG, "set no touch screen timeout:" + NO_TOUCH_CNT_TIMEOUT); + + //mHandler.postDelayed(noTouchCheckRunnable, TS_CHECK_LOOP_TIME1); + + } catch (Exception e) { + Log.e(TAG, "error:\n" + e); + e.printStackTrace(); + } + + //mSurfaceFlinger = ServiceManager.getService("android.ui.ISurfaceComposer"); + //try { + // final Parcel data = Parcel.obtain(); + // data.writeInterfaceToken("android.ui.ISurfaceComposer"); + // data.writeInt(1); + // mSurfaceFlinger.transact(1008, data, null, 0); + // Log.d(TAG, "----------------- run mSurfaceFlinger.transact(1008)"); + //}catch (RemoteException ex) { + // Log.d(TAG, "----------------- mSurfaceFlinger.transact error"); + //} + } + + @Override + public void onDestroy() { + unregisterReceiver(mReceiver); + unregisterReceiver(mReceiver2); + super.onDestroy(); + } + + private final BroadcastReceiver mReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + try { + String action = intent.getAction(); + if(action.equals(ACTION_APK_INSTALL)) { + String path = intent.getStringExtra("apkFilePath"); + if (path != null) { + installPackageName = getPackage(mContext,path); + if (installPackageName != null) { + if (intent.getBooleanExtra("allPrms",false)) { + SystemProperties.set("persist.app.grant.prms",installPackageName); + } + silentInstallApk(path); + if (intent.getBooleanExtra("autostart",false)) { + installedStartActivityFlag = true; + } + } + } + Log.d(TAG,"start install apk name:" + installPackageName); + } else if(action.equals(ACTION_TOUCH_TEST)) { + int enable = intent.getIntExtra("enable",0); + + if(DEBUG) Log.d(TAG,"touch test enable:" + enable); + + if (enable == 1) { + //Settings.Global.getInt(mContext.getContentResolver(), + //Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 1); //开启开发者模式 + + Settings.System.putInt(mContext.getContentResolver(),//开启指针位置 + Settings.System.POINTER_LOCATION, 1); + } else { + //Settings.Global.putInt(mContext.getContentResolver(),//关闭开发者模式 + //Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0); + + Settings.System.putInt(mContext.getContentResolver(),//开启指针位置 + Settings.System.POINTER_LOCATION, 0); + } + } else if(action.equals(ACTION_IDO_SET_SHUTDOWN)) { + systemShutdown(intent.getBooleanExtra("confirm",true)); + //systemShutdown(false); + } else if(action.equals(ACTION_IDO_SET_REBOOT)) { + systemReboot(intent.getBooleanExtra("confirm",true)); + } else if(action.equals(ACTION_IDO_SET_FACTORY)) { + doMasterClear(); + } else if(action.equals(ACTION_IDO_SET_AIRPLANE_MODE)) { + setAirPlaneMode(intent.getBooleanExtra("enable",false)); + } else if(action.equals(ACTION_IDO_SET_APP_CHECK)) { + if (intent.getBooleanExtra("enable",false)) { + checkAppErrCnt = 0; + enableAppCheck = true; + mHandler.removeCallbacks(appCheckRunnable); + mHandler.postDelayed(appCheckRunnable, CHECK_APP_LOOP_TIME); + + checkPackageName = intent.getStringExtra("packageName"); + runClassName = intent.getStringExtra("className"); + checkAppCnt = intent.getIntExtra("checkCnt",3);//unit:2s,//检测APP是否在界面显示的次数,大于此次数还没有发现APP在前端运行则重启app,默认3次 + + SystemProperties.set("persist.ido.appcheck.packageName",checkPackageName); + SystemProperties.set("persist.ido.appcheck.className",runClassName); + SystemProperties.set("persist.ido.appcheck.checkCnt",""+checkAppCnt); + SystemProperties.set("persist.ido.appcheck.enable",""+enableAppCheck); + } else { + enableAppCheck = false; + checkAppErrCnt = 0; + mHandler.removeCallbacks(appCheckRunnable); + SystemProperties.set("persist.ido.appcheck.enable",""+enableAppCheck); + } + } else if(action.equals(ACTION_IDO_SCREEN_SHOT)) { + String savePath = intent.getStringExtra("path"); + String id = intent.getStringExtra("id"); + if (savePath != null && id != null) + screenshot(savePath,id); + else if (savePath != null && id == null) + screenshot(savePath,"0"); + } else if(action.equals(ACTION_IDO_SET_TIME)) { + int[] time = intent.getIntArrayExtra("time"); + + int year = time[0]; + int month = time[1] - 1; + int day = time[2]; + int hour = time[3]; + int minute = time[4]; + int second = time[5]; + + setDateAndTime(year,month,day,hour,minute,second); + } else if(action.equals(ACTION_IDO_SET_WIFIAP)) { + mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); + if (mWifiManager != null) + mWifiConfig = mWifiManager.getWifiApConfiguration(); + + if (mWifiManager != null && intent.getBooleanExtra("enable",false)) { + if (mWifiConfig == null) { + mWifiConfig = new WifiConfiguration(); + + String ssid = intent.getStringExtra("name"); + if (ssid != null && ssid.length() != 0) { + mWifiConfig.SSID = ssid; + } else { + mWifiConfig.SSID = "wifiAp";//默认名称 + } + + mWifiConfig.apBand = 0; + + mWifiConfig.allowedKeyManagement.set(KeyMgmt.WPA2_PSK); + mWifiConfig.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN); + + String mPassword = intent.getStringExtra("password"); + if (mPassword != null && mPassword.length() >= 8) { + mWifiConfig.preSharedKey = mPassword; + + mWifiManager.setWifiApConfiguration(mWifiConfig); + + if (wifiApState == WifiManager.WIFI_AP_STATE_ENABLED) { + Log.d(TAG, "wifi ap is enabled,first stop ap!\n"); + mWifiManager.stopSoftAp(); + restartWifiApFlag = true; + } else if (mWifiManager.startSoftAp(mWifiConfig)) { + Log.d(TAG, "Start wifi ap ok!\n"); + } else { + Log.d(TAG, "Start ap failed!\n"); + } + } else { + Log.e(TAG, "Set Wifi ap password length error!\n"); + } + } else { + String ssid = intent.getStringExtra("name"); + if (ssid != null && ssid.length() != 0) { + mWifiConfig.SSID = ssid; + } else { + mWifiConfig.SSID = "wifiAp";//默认名称 + } + + mWifiConfig.apBand = 0; + + mWifiConfig.allowedKeyManagement.set(KeyMgmt.WPA2_PSK); + mWifiConfig.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN); + + String mPassword = intent.getStringExtra("password"); + if (mPassword != null && mPassword.length() >= 8) { + mWifiConfig.preSharedKey = mPassword; + + mWifiManager.setWifiApConfiguration(mWifiConfig); + + if (wifiApState == WifiManager.WIFI_AP_STATE_ENABLED) { + Log.d(TAG, "wifi ap is enabled,first stop ap!\n"); + mWifiManager.stopSoftAp(); + restartWifiApFlag = true; + } else if (mWifiManager.startSoftAp(mWifiConfig)) { + Log.d(TAG, "Start wifi ap ok!\n"); + } else { + Log.d(TAG, "Start ap failed!\n"); + } + } else { + Log.e(TAG, "Set Wifi ap password length error!\n"); + } + } + } else if (mWifiManager != null) { + if (wifiApState == WifiManager.WIFI_AP_STATE_ENABLED && mWifiManager.stopSoftAp()) { + if(DEBUG) Log.d(TAG, "Stop wifi ap ok!\n"); + } else if (wifiApState == WifiManager.WIFI_AP_STATE_DISABLED) { + Log.d(TAG, "wifi ap default is disabled!\n"); + } else { + Log.d(TAG, "Stop wifi ap failed!\n"); + } + } else { + Log.e(TAG, "Wifi manager is null!\n"); + } + } else if (WifiManager.WIFI_AP_STATE_CHANGED_ACTION.equals(action)) { + int state = intent.getIntExtra( + WifiManager.EXTRA_WIFI_AP_STATE, WifiManager.WIFI_AP_STATE_FAILED); + if (state == WifiManager.WIFI_AP_STATE_FAILED) { + int reason = intent.getIntExtra(WifiManager.EXTRA_WIFI_AP_FAILURE_REASON, + WifiManager.SAP_START_FAILURE_GENERAL); + handleWifiApStateChanged(state, reason); + } else { + handleWifiApStateChanged(state, WifiManager.SAP_START_FAILURE_GENERAL); + } + } else if(action.equals(ACTION_IDO_SET_SCREEN)) { + String scState = intent.getStringExtra("screen"); + if (mDevCtrl != null && scState != null && scState.equals("off")) { + //blBrightness = getSystemBrightness(); + mDevCtrl.set_backlight_brightness(0);//关闭LCD背光 + setUserLed(1,0);//关闭编码器灯 + noTouchDataCnt = NO_TOUCH_CNT_TIMEOUT - 1; + } else if (mDevCtrl != null && scState != null && scState.equals("on")) { + mDevCtrl.set_backlight_brightness(getSystemBrightness()); + setUserLed(1,1);//开启编码器灯 + noTouchDataCnt = 0; + } + + String tpState = intent.getStringExtra("tp"); + if (tpState != null && tpState.equals("off")) { + setDisableTs(true); + } else if (tpState != null && tpState.equals("on")) { + setDisableTs(false); + } + + int time = intent.getIntExtra("timeout",2147483600); + + if (time != 2147483600) { + noTouchDataCnt = 0; + NO_TOUCH_CNT_TIMEOUT = time; + SystemProperties.set("persist.ido.screen.timeout",""+NO_TOUCH_CNT_TIMEOUT); + Log.d(TAG, "set no touch screen timeout:" + NO_TOUCH_CNT_TIMEOUT); + } + } else if (action.equals(ACTION_IDO_SET_SCREEN_BRIGHTNESS)) { + setSystemBrightness(intent.getIntExtra("value",200)); + } else if (action.equals(ACTION_IDO_SET_TIMEOUT_SLEEP)) { + int time = intent.getIntExtra("time",-1); + if (time == -1) { + Settings.System.putInt(mContext.getContentResolver(), SCREEN_OFF_TIMEOUT, (int)2147483647); + } else if (time != 0) { + Settings.System.putInt(mContext.getContentResolver(), SCREEN_OFF_TIMEOUT, (int)time*1000); + } + } else if (action.equals(ACTION_IDO_GET_TIMEOUT_SLEEP)) { + final long displayTimeout = Math.max(0,Settings.System.getInt(mContext.getContentResolver(), SCREEN_OFF_TIMEOUT, 0)); + Intent intent1 = new Intent("android.ido.action.sleep.time"); + intent1.putExtra("time",(int)(displayTimeout/1000)); + mContext.sendBroadcast(intent1); + } else if (ACTION_IDO_SET_LAUNCHER.equals(action)) { + String launcherPackageName = intent.getStringExtra("packageName"); + String launcherClassName = intent.getStringExtra("className"); + + if (launcherPackageName != null && launcherClassName != null) { + SystemProperties.set("persist.ido.launcher.packagename",launcherPackageName); + SystemProperties.set("persist.ido.launcher.classname",launcherClassName); + } + } else if(action.equals(ACTION_IDO_SET_SCREEN_ONOFF_GET)) { + Intent intent1 = new Intent("android.ido.action.screenoff.time"); + intent1.putExtra("timeout",NO_TOUCH_CNT_TIMEOUT); + mContext.sendBroadcast(intent1); + } else if(action.equals(ACTION_IDO_SET_ADB)) { + if (intent.getBooleanExtra("enable",false)) { + Settings.Secure.putInt(mContext.getContentResolver(),Settings.Global.ADB_ENABLED,1); + } else { + Settings.Secure.putInt(mContext.getContentResolver(),Settings.Global.ADB_ENABLED,0); + } + } else if(action.equals(ACTION_IDO_SET_INPUT)) { + setInputReport(); + } + } catch (Exception e) { + if(DEBUG) Log.d(TAG, "BroadcastReceiver error:\n" + e); + } + } + }; + + private void setSystemBrightness(int val) { + try { + Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS,val); + } catch (Exception e) { + Log.e(TAG, "setSystemBrightness error:\n" + e); + } + } + + private final BroadcastReceiver mReceiver2 = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + try { + if(intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) { + + String data = intent.getDataString(); + + if(data == null || data.length() <= PACKAGE_NAME_START_INDEX) + { + return; + } + + String packageName = data.substring(PACKAGE_NAME_START_INDEX); + + Log.d(TAG,"apk installed package name:" + packageName + ",auto start:" + installedStartActivityFlag); + + Intent intent1 = new Intent(ACTION_IDO_GET_APK_INSTALLED); + intent1.putExtra("packageName",packageName); + mContext.sendBroadcast(intent1); + + if(installedStartActivityFlag && packageName.equals(installPackageName)) + { + SystemProperties.set("persist.ido.launcher.kill","true"); + startApkActivity(context,installPackageName); + installPackageName = null; + installedStartActivityFlag = false; + SystemProperties.set("persist.ido.launcher.kill","false"); + } + } /*else if(intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)) { + + String data = intent.getDataString(); + + if(data == null || data.length() <= PACKAGE_NAME_START_INDEX) + { + return; + } + + String packageName = data.substring(PACKAGE_NAME_START_INDEX); + + if(packageName.equals(installPackageName) ) + { + startApkActivity(context,installPackageName); + } + + if(DEBUG) Log.d(TAG,"replace installed apk name:" + packageName); + }*/ + } catch (Exception e) { + Log.e(TAG, "" + e); + } + } + }; + + private void handleWifiApStateChanged(int state, int reason) { + switch (state) { + case WifiManager.WIFI_AP_STATE_ENABLED: + wifiApState = WifiManager.WIFI_AP_STATE_ENABLED; + break; + case WifiManager.WIFI_AP_STATE_DISABLED: + wifiApState = WifiManager.WIFI_AP_STATE_DISABLED; + + if (restartWifiApFlag) { + restartWifiApFlag = false; + if (mWifiManager != null && mWifiManager.startSoftAp(mWifiConfig)) + Log.d(TAG, "Start wifi ap ok!\n"); + else + Log.d(TAG, "Start ap failed!\n"); + } + break; + } + } + + private Handler mHandler = new Handler(); + private Runnable apkRunnable = new Runnable() { + @Override + public void run() { + try { + Intent intent = new Intent(); + if (installPackageName != null && installClassName != null) { + intent.setClassName(installPackageName,installClassName); + startActivity(intent); + } + } catch (Exception e) { + Log.e(TAG, e.toString()); + } + } + }; + + private Runnable appCheckRunnable = new Runnable() { + @Override + public void run() { + try { + String processActivity = ""; + ActivityManager manager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE); + //获取正在运行的task列表,其中1表示最近运行的task,通过该数字限制列表中task数目,最近运行的靠前 + List<ActivityManager.RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1); + + if (runningTaskInfos != null && runningTaskInfos.size() != 0) { + processActivity = (runningTaskInfos.get(0).baseActivity).getPackageName(); + if(DEBUG) Log.d(TAG, "Current Activity Package Name:" + processActivity); + } + + if(DEBUG) Log.d(TAG, "Check app Package Name:" + checkPackageName); + if(DEBUG) Log.d(TAG, "Check app Class Name:" + runClassName); + + if (checkPackageName != null && processActivity.compareTo(checkPackageName) != 0) { + checkAppErrCnt++; + if (checkAppCnt > 0 && checkAppErrCnt >= checkAppCnt) { + checkAppErrCnt = 0; + + Intent intent = new Intent(); + if (checkPackageName != null && runClassName != null && checkPackageName.equals("") == false && runClassName.equals("") == false) { + intent.setClassName(checkPackageName,runClassName); + startActivity(intent); + } else { + Log.e(TAG, "checkAppErr,packageName:" + checkPackageName + ",ClassName" + runClassName); + enableAppCheck = false; + } + } + } else { + checkAppErrCnt = 0; + } + + if(DEBUG) Log.d(TAG, "checkAppErrCnt:" + checkAppErrCnt); + + } catch (Exception e) { + Log.e(TAG, e.toString()); + } + + if (enableAppCheck && appCheckRunnable != null) + mHandler.postDelayed(appCheckRunnable, CHECK_APP_LOOP_TIME); + } + }; + + private Runnable noTouchCheckRunnable = new Runnable() { + @Override + public void run() { + try { + int temp = getTsCnt(); + if (lastTsCnt != temp) { + lastTsCnt = temp; + noTouchDataCnt = 0; + if (onTouchFlag == 1) { + onTouchFlag = 0; + setUserLed(1,1);//开启编码器灯 + setDisableTs(false); + Log.d(TAG,"====>has touch on backlight!!!"); //开启LCD背光 + if (mDevCtrl != null) { + mDevCtrl.set_backlight_brightness(getSystemBrightness()); + Intent intent1 = new Intent("android.ido.action.screenon.now"); + //intent1.putExtra("timeout",NO_TOUCH_CNT_TIMEOUT); + mContext.sendBroadcast(intent1); + ts_check_loop_time = TS_CHECK_LOOP_TIME1; + } + } + } else { + noTouchDataCnt++; + + if ((NO_TOUCH_CNT_TIMEOUT != -1) && (noTouchDataCnt == NO_TOUCH_CNT_TIMEOUT)) { + if (onTouchFlag == 0) { + onTouchFlag = 1; + //关闭LCD背光 + setUserLed(1,0);//关闭编码器灯 + if (mDevCtrl != null) { + //blBrightness = getSystemBrightness(); + mDevCtrl.set_backlight_brightness(0);//关闭LCD背光 + Intent intent1 = new Intent("android.ido.action.screenoff.now"); + intent1.putExtra("timeout",NO_TOUCH_CNT_TIMEOUT); + mContext.sendBroadcast(intent1); + ts_check_loop_time = TS_CHECK_LOOP_TIME2; + } + } + Log.d(TAG,"====>no touch timeout!!!"); + } + } + mHandler.postDelayed(noTouchCheckRunnable, ts_check_loop_time); + } catch (Exception e) { + Log.e(TAG, e.toString()); + } + } + }; + + private int getSystemBrightness() { + int systemBrightness = 0; + try { + systemBrightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS); + } catch (Exception e) { + e.printStackTrace(); + } + return systemBrightness; + } + + //confirm:ture-会弹出是否关机的确认窗口,false-无弹框,直接关机 + private void systemShutdown(boolean confirm) { + Intent intent = new Intent("com.android.internal.intent.action.REQUEST_SHUTDOWN"); + intent.putExtra(Intent.EXTRA_KEY_CONFIRM, confirm); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + startActivity(intent); + + /*PowerManager mPowerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE); + if (mPowerManager != null) + mPowerManager.shutdown(false, "user shutdwon",false);*/ + } + + //confirm:ture-会弹出是否关机的确认窗口,false-无弹框,直接重启 + private void systemReboot(boolean confirm) { + Intent intent = new Intent("android.intent.action.REBOOT"); + intent.putExtra(Intent.EXTRA_KEY_CONFIRM, confirm); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + startActivity(intent); + } + + private void doMasterClear() {//恢复出厂设置 + Intent intent = new Intent(Intent.ACTION_FACTORY_RESET); + intent.setPackage("android"); + intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); + intent.putExtra(Intent.EXTRA_REASON, "FactoryMode"); + intent.putExtra(Intent.EXTRA_WIPE_EXTERNAL_STORAGE, true); + intent.putExtra(Intent.EXTRA_WIPE_ESIMS, true); + mContext.sendBroadcast(intent); + } + + private void setAirPlaneMode(boolean enable) {//设置开启/关闭飞行模式 + if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN) { + Settings.System.putInt(mContext.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, enable ? 1 : 0); + } else { + Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, enable ? 1 : 0); + } + Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); + intent.putExtra("state", enable); + mContext.sendBroadcast(intent); + } + + private void silentInstallApk(String path) {//安装的包可以卸载 + if(DEBUG) Log.d(TAG, "silentInstallApk start"); + + if (path == null) { + Log.e(TAG,"Apk paht is null!"); + return; + } + + if (isApkFile(mContext,path)) { + if(DEBUG) Log.d(TAG,"Installing:" + path); + try{ + InstallUtils.install28(mContext,path,InstallResultReceiver.class); + } catch (Exception e) { + Log.e(TAG,"silentInstallApk Error:" + e); + } + } else { + Log.e(TAG,path + " is invaild apk!"); + } + } + + private void screenshot(String savePath,String id) { + Process process = null; + BufferedReader successResult = null; + BufferedReader errorResult = null; + StringBuilder successMsg = new StringBuilder(); + StringBuilder errorMsg = new StringBuilder(); + try { + process = new ProcessBuilder("/system/bin/screencap","-d",id,"-p", savePath).start(); + successResult = new BufferedReader(new InputStreamReader(process.getInputStream())); + errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream())); + String s; + while ((s = successResult.readLine()) != null) { + successMsg.append(s); + if(DEBUG) Log.d(TAG,"screencap:" + savePath); + } + while ((s = errorResult.readLine()) != null) { + errorMsg.append(s); + Log.e(TAG,"screencap:" + savePath+",Error:"+errorMsg.toString()); + } + } catch (Exception e) { + Log.e(TAG,e.toString()); + } + } + + public static String getPackage(Context context, String filePath) { + PackageManager pm = context.getPackageManager(); + + PackageInfo info = pm.getPackageArchiveInfo(filePath, PackageManager.GET_ACTIVITIES); + + if (info != null) + return info.packageName; + else + return null; + } + + public static void startApkActivity(final Context ctx, String packageName) { + PackageManager pm = ctx.getPackageManager(); + PackageInfo pi; + try { + pi = pm.getPackageInfo(packageName, 0); + Intent intent = new Intent(Intent.ACTION_MAIN, null); + intent.addCategory(Intent.CATEGORY_LAUNCHER); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + intent.setPackage(pi.packageName); + + List<ResolveInfo> apps = pm.queryIntentActivities(intent, 0); + + ResolveInfo ri = apps.iterator().next(); + if (ri != null) { + String className = ri.activityInfo.name; + intent.setComponent(new ComponentName(packageName, className)); + ctx.startActivity(intent); + } + } catch (Exception ex) { + Log.e(TAG, ex.toString()); + } + } + + private void setDateAndTime(int year, int month, int day,int hourOfDay, int minute,int second) { + Calendar c = Calendar.getInstance(); + + c.set(Calendar.YEAR, year); + c.set(Calendar.MONTH, month); + c.set(Calendar.DAY_OF_MONTH, day); + c.set(Calendar.HOUR_OF_DAY, hourOfDay); + c.set(Calendar.MINUTE, minute); + c.set(Calendar.SECOND, second); + c.set(Calendar.MILLISECOND, 0); + + long when = c.getTimeInMillis(); + + if (when / 1000 < Integer.MAX_VALUE && mAlarmManager != null) { + mAlarmManager.setTime(c.getTimeInMillis()); + } + } + + private static void setDisableTs(boolean on) { //设置禁止触摸上报数据 + final String path = "/dev/dis_ts"; + if (new File(path).exists()) { + FileWriter writer = null; + try { + writer = new FileWriter(path); + if (on) { + writer.write("on "); + } else { + writer.write("off "); + } + writer.flush(); + } catch (Exception ex) { + Log.d(TAG, "" + ex); + } finally { + if (writer != null) { + try { + writer.close(); + } catch (IOException ex) { + } + } + } + } + } + + private static int getTsCnt() {//获取触摸上报的记数,触摸一次变化一个数据 + byte[] buff = new byte[3]; + int tsCount; + final String IDO_TS = "/dev/dis_ts";; + if (new File(IDO_TS).exists()) { + try { + FileInputStream tsDev = new FileInputStream(new File(IDO_TS)); + tsDev.read(buff, 0, 3); + tsCount = (buff[0] & 0xFF) * 256 + (buff[1] & 0xFF); + tsDev.close(); + //Log.d(TAG,"Ts Count:" + tsCount); + return tsCount; + } catch (Exception ex) { + Log.d(TAG, "" + ex); + return 0; + } + } + return 0; + } + + private static void setInputReport() { //上报1,1坐标为重置休眠计数 + final String path = "/dev/dis_ts"; + if (new File(path).exists()) { + FileWriter writer = null; + try { + writer = new FileWriter(path); + writer.write("inp "); + writer.flush(); + } catch (Exception ex) { + Log.d(TAG, "" + ex); + } finally { + if (writer != null) { + try { + writer.close(); + } catch (IOException ex) { + } + } + } + } + } + + //on=1,LED灯亮 + //on=0, LED灯灭 + synchronized private static void setUserLed(int id, int on) { + String path = "/sys/class/leds/led" + id + "/brightness"; + if (new File(path).exists()) { + final String filename = path; + FileWriter writer = null; + try { + writer = new FileWriter(filename); + if (on == 0) { + writer.write("0"); + } else { + writer.write("1"); + } + writer.flush(); + } catch (Exception ex) { + Log.d(TAG, "" + ex); + } finally { + if (writer != null) { + try { + writer.close(); + } catch (IOException ex) { + } + } + } + } + } + + private static void setPowerSleep() { + String path = "/sys/power/state"; + if (new File(path).exists()) { + final String filename = path; + FileWriter writer = null; + try { + writer = new FileWriter(filename); + writer.write("mem"); + writer.flush(); + } catch (Exception ex) { + Log.d(TAG, "" + ex); + } finally { + if (writer != null) { + try { + writer.close(); + } catch (IOException ex) { + } + } + } + } + } + + private static Boolean isApkFile(Context context, String filePath) { + String pkg; + + try { + PackageManager mPackageManager = context.getPackageManager(); + PackageInfo info = mPackageManager.getPackageArchiveInfo(filePath, PackageManager.GET_ACTIVITIES); + if (info !=null) { + ApplicationInfo appInfo = info.applicationInfo; + pkg=appInfo.packageName; + } else { + pkg=""; + } + }catch (Exception e) { + pkg=""; + } + return !TextUtils.isEmpty(pkg); + } + + + private boolean fileIsExists(String strFile) { + try + { + File f=new File(strFile); + if(!f.exists()) + { + return false; + } + } + catch (Exception e) + { + return false; + } + + return true; + } +} diff --git a/android/packages/apps/Settings/src/com/android/settings/InstallResultReceiver.java b/android/packages/apps/Settings/src/com/android/settings/InstallResultReceiver.java new file mode 100755 index 0000000..4561592 --- /dev/null +++ b/android/packages/apps/Settings/src/com/android/settings/InstallResultReceiver.java @@ -0,0 +1,48 @@ +package com.android.settings; +import android.app.PendingIntent; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.pm.PackageInstaller; +import android.util.Log; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +public class InstallResultReceiver extends BroadcastReceiver { + private String TAG = "myLog"; + + @Override + public void onReceive(Context context, Intent intent) { + + if (intent != null) { + + final int status = intent.getIntExtra(PackageInstaller.EXTRA_STATUS, + PackageInstaller.STATUS_FAILURE); + String packageName = null; + + try { + packageName = intent.getData().getSchemeSpecificPart(); + }catch (Exception e){ + e.printStackTrace(); + } + + Log.e(TAG, "action="+intent.getAction()+" ,status="+status); + + if (status == PackageInstaller.STATUS_SUCCESS) { + // success + Log.e(TAG, packageName + " install success, msg: "+intent.getStringExtra(PackageInstaller.EXTRA_STATUS_MESSAGE)); + } else { + Log.e(TAG, packageName + " install filed, msg: "+intent.getStringExtra(PackageInstaller.EXTRA_STATUS_MESSAGE)); + } + } + } +} + + + + + diff --git a/android/packages/apps/Settings/src/com/android/settings/InstallUtils.java b/android/packages/apps/Settings/src/com/android/settings/InstallUtils.java new file mode 100755 index 0000000..2689a23 --- /dev/null +++ b/android/packages/apps/Settings/src/com/android/settings/InstallUtils.java @@ -0,0 +1,142 @@ +//调用方法为: +//InstallUtils.install28(MainActivity.this,"/data/local/tmp/test.apk",InstallResultReceiver.class) + +//需系统签名和添加以下权限 +//<uses-permission android:name="android.permission.INSTALL_PACKAGES" /> +//<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> + +package com.android.settings; +import android.app.PendingIntent; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.pm.PackageInstaller; +import android.util.Log; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + + +public class InstallUtils { + private static final String TAG = "myLog"; + + // 适配android9的安装方法。 + public static void install28(Context context, String apkFilePath,Class<InstallResultReceiver> receiver) { + + Log.d(TAG,"install28 path="+apkFilePath); + + File apkFile = new File(apkFilePath); + + PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller(); + + PackageInstaller.SessionParams sessionParams + = new PackageInstaller.SessionParams(PackageInstaller + .SessionParams.MODE_FULL_INSTALL); + + sessionParams.setSize(apkFile.length()); + int sessionId = createSession(packageInstaller, sessionParams); + + Log.d(TAG,"install28 sessionId="+sessionId); + + if (sessionId != -1) { + boolean copySuccess = copyInstallFile(packageInstaller, sessionId, apkFilePath); + Log.d(TAG,"install28 copySuccess="+copySuccess); + if (copySuccess) { + execInstallCommand(context,packageInstaller, sessionId,receiver); + } + } + } + + private static int createSession(PackageInstaller packageInstaller, + PackageInstaller.SessionParams sessionParams) { + + int sessionId = -1; + + try { + sessionId = packageInstaller.createSession(sessionParams); + } catch (IOException e) { + e.printStackTrace(); + } + return sessionId; + } + + private static boolean copyInstallFile(PackageInstaller packageInstaller,int sessionId, String apkFilePath) { + + InputStream in = null; + OutputStream out = null; + PackageInstaller.Session session = null; + boolean success = false; + + try { + + File apkFile = new File(apkFilePath); + session = packageInstaller.openSession(sessionId); + out = session.openWrite("base.apk", 0, apkFile.length()); + in = new FileInputStream(apkFile); + + int total = 0, c; + byte[] buffer = new byte[65536]; + while ((c = in.read(buffer)) != -1) { + total += c; + out.write(buffer, 0, c); + } + + session.fsync(out); + Log.i(TAG, "streamed " + total + " bytes"); + success = true; + } catch (IOException e) { + e.printStackTrace(); + } finally { + + try { + if (out!=null){ + out.close(); + } + + if (in!=null){ + in.close(); + } + + if (session!=null){ + session.close(); + } + + } catch (IOException e) { + e.printStackTrace(); + } + } + + return success; + } + + + private static void execInstallCommand(Context context, PackageInstaller packageInstaller, int sessionId, Class<InstallResultReceiver> receiver) { + + PackageInstaller.Session session = null; + try { + session = packageInstaller.openSession(sessionId); + Intent intent = new Intent(context, receiver); + PendingIntent pendingIntent = PendingIntent.getBroadcast(context, + 1, intent, + PendingIntent.FLAG_UPDATE_CURRENT); + + session.commit(pendingIntent.getIntentSender()); + + Log.i(TAG, "begin session"); + + } catch (IOException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } finally { + session.close(); + } + } +} + + + + diff --git a/android/packages/apps/Settings/src/com/android/settings/MyBootReceiver.java b/android/packages/apps/Settings/src/com/android/settings/MyBootReceiver.java new file mode 100755 index 0000000..3ea41e4 --- /dev/null +++ b/android/packages/apps/Settings/src/com/android/settings/MyBootReceiver.java @@ -0,0 +1,20 @@ +package com.android.settings; +//import android.settings.development; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; + +public class MyBootReceiver extends BroadcastReceiver { + + @Override + public void onReceive(Context context, Intent intent) { + // TODO Auto-generated method stub + String action = intent.getAction(); + if (action.equals(Intent.ACTION_BOOT_COMPLETED)) + { + Intent service = new Intent(context, IdoDevCustomService.class); + context.startService(service); + } + } + +} -- Gitblit v1.6.2