provider for back&restore app datya

空白的泡發表於2020-12-02

 

back resotre app data, inf

///**
//1.  概述:由客戶端自行查詢應用並展示給使用者,使用者選擇需要備份或還原的應用後,依次通過 provider insert 介面進行備份還原的操作(每一個應用執行一次 insert)
//2.  Provider URI:content://com.ll.SecurityCenter.backuprestore/app
//3.  ContentValues格式:
//package_name: 需要備份或還原應用的包名
//patch:需要備份或還原的內容存放路徑
//data:0 為不需要備份或還原應用資料,1 為需要備份或還原應用資料
//action:0為備份,1 為還原
//4.      舉例:
//item = new ContentValues();
//item.put("package_name","com.meitu.meiyancamera");
//item.put("path", "/sdcard/zzzzz/");
//**/
//
//public class AppProvider extends ContentProvider {
//
//    private static final String TAG = "AppProvider";
//    private static final UriMatcher mMatcher;
//    //AUTHORITY
//    private static final String AUTHORITY = "com.ll.SecurityCenter.backuprestore";
//    private static final String APP_DATA_PATH_CFG_FILE = "/sdcard/.appDataPath.cfg";
//    private static final String BACKUP_APP_DATA_CFG_FILE = "/sdcard/.backupAppData.cfg";
//    private static final String RESTORE_APP_DATA_CFG_FILE = "/sdcard/.restoreAppData.cfg";
//    private static final String PROGRESS_FLAG_FILE = "/sdcard/.progressFlagFile";
//    public static final int SUCCESS = 0;
//    public static final int ERR = 1;
//    public static final int UNKNOWN_ERR = -1;
//
//    private static final int AppBackupRestore = 1;
//
//    static {
//        mMatcher = new UriMatcher(UriMatcher.NO_MATCH);
//        mMatcher.addURI(AUTHORITY, "app", AppBackupRestore);   //com.ll.SecurityCenter.backuprestore/app
//    }
//
//    //================================主要操作程式碼=========================================== start
//    @Override
//    public Uri insert(Uri uri, ContentValues values) {
//        Uri result = null;
//        switch (mMatcher.match(uri)) {
//
//        // 對應用程式備份恢復的操作
//        case AppBackupRestore:
//            String action = (String) values.get("action");
//            String data = (String) values.get("data");
//            Log.d(TAG, "insert  action:" + action + ",  data :" + data);
//            //backup
//            if (action.equals("0")) {
//                result = doAppBackup(values);
//            } 
//            //restore
//            else if (action.equals("1")) {
//                result = doAppRestore(values);
//            }
//            break;
//
//        default:
//            break;
//        }
//        return result;
//    }
//
//    public Uri doAppBackup(ContentValues values){
//        String backupPatch = (String) values.get("path");
//        String data =  (String) values.get("data");
//        String packageName = (String) values.get("package_name");
//        Log.d(TAG, "packageName : "+ packageName + ", backupPatch:" + backupPatch);
//        if(data.equals("1")){
//            File pathFile1 = new File(APP_DATA_PATH_CFG_FILE);
//            if (pathFile1.exists()) {
//                pathFile1.delete();
//            }
//            File packageFile1 = new File(BACKUP_APP_DATA_CFG_FILE);
//            if (packageFile1.exists()) {
//                packageFile1.delete();
//            }
//            FileOutputStream outPath = null;
//            FileOutputStream outPackage = null;
//            try {
//                File pathFile = new File(APP_DATA_PATH_CFG_FILE);
//                File packageFile = new File(BACKUP_APP_DATA_CFG_FILE);
//                outPath = new FileOutputStream(pathFile, true);
//                outPackage = new FileOutputStream(packageFile, true);
//                String useablePath = backupPatch;
//                if (useablePath.startsWith("/storage/emulated/0") ) {
//                    useablePath = useablePath.replaceFirst("/storage/emulated/0","/sdcard");
//                }
//                outPath.write(useablePath.getBytes());
//                Log.d(TAG, "useablePath = " + useablePath + ", PackageName = " + packageName);
//                outPackage.write(packageName.getBytes());
//                outPackage.write('\n');
//
//            } catch (Exception e) {
//                Log.d(TAG, "backupOneAPP  PackageName:"+  packageName + "error:" +  e.toString());
//                return null;
//            } finally {
//                if (null != outPath) {
//                    try {
//                        outPath.flush();
//                        outPath.close();
//                    } catch (Exception e) {
//                        outPath = null;
//                    }
//                }
//                if (null != outPackage) {
//                    try {
//                        outPackage.flush();
//                        outPackage.close();
//                    } catch (Exception e) {
//                        outPackage = null;
//                    }
//                }
//            }
//           SystemService.start("backupAppData");
//           boolean whileFlag = true;
//           while (whileFlag) {
//               try {
//                   Thread.sleep(1000);
//               } catch (Exception e) {
//                   Log.e(TAG, "InterruptedException :" + e);
//               }
//               File flagFile = new File(PROGRESS_FLAG_FILE);
//               if (!flagFile.exists()) {
//                   whileFlag = false;
//               }
//               flagFile = null;
//           }
//           //File apkFile = new File(backupDir + "/" + appin.packageName+ ".apk");
//           File dataFile = new File(backupPatch + "/" + packageName+ ".tar");
//           if(dataFile.exists()){
//               return Uri.fromFile(dataFile);
//           }else{
//               return null;
//           }
//        }else{
//            return null;
//        }
//    }
//
//    public Uri doAppRestore(ContentValues values){
//        String restorePatch = (String) values.get("path");
//        String data =  (String) values.get("data");
//        String packageName = (String) values.get("package_name");
//        Log.d(TAG, "restorePatch = " + restorePatch + ", data = " + data + ", packageName:"+ packageName);
//        Uri result = null;
//        if(data.equals("1")){
//            //restore appdata
//            result = restoreAppData(restorePatch,packageName);
//        }else if(data.equals("0")){
//            //restore app
//            String fullApkName = restorePatch + "/"+ packageName + ".apk";
//            result =installApk(fullApkName);
//        }
//        return  result;
//    }
//
//    private Uri installApk(String fullApkName) {
//        Uri result = Uri.parse(fullApkName);
//        Log.d(TAG, "Install App: " + fullApkName);
//        String  newcommand =  "pm  install   -r    -i  "+   "com.android.packageinstaller"+  "  --user 0   "+ fullApkName;
//        Process process;
//        try {
//            process = Runtime.getRuntime().exec(newcommand);
//            process.waitFor();
//        } catch (IOException e) {
//            result = null;
//            Log.d(TAG, "install apk Name:"+ fullApkName + ",  IOException:"+ e.toString());
//        } catch (Exception e) {
//            result = null;
//            Log.d(TAG, "install apk Name:"+ fullApkName + ",  Exception:"+ e.toString());
//        }
//        Log.d(TAG, "install result: " + result);
//        return result;
//    }
//
//    private Uri restoreAppData(String restorePath, String packageName) {
//        Uri  ret = Uri.parse(packageName);
//        File pathFile = new File(APP_DATA_PATH_CFG_FILE);
//        if(pathFile.exists()){
//            pathFile.delete();
//        }
//        File packageFile = new File(RESTORE_APP_DATA_CFG_FILE);
//        if(packageFile.exists()){
//            packageFile.delete();
//        }
//        FileOutputStream outPath = null;
//        FileOutputStream outPackage = null;
//        try {
//            outPath = new FileOutputStream(APP_DATA_PATH_CFG_FILE, true);
//            outPackage = new FileOutputStream(RESTORE_APP_DATA_CFG_FILE, true);
//            String useablePath = restorePath;
//            if (restorePath.startsWith("/storage/emulated/0")) {
//                useablePath = restorePath.replaceFirst("/storage/emulated/0", "/sdcard");
//            }
//            outPath.write(useablePath.getBytes());
//            outPackage.write(packageName.getBytes());
//            outPackage.write('\n');
//            Log.d(TAG, "  RECOVERAPPDATA------->:"+ packageName);
//            SystemService.start("recoverAppData");
//            boolean whileFlag = true;
//            while (whileFlag) {
//                try {
//                    Thread.sleep(1500);
//                } catch (InterruptedException e) {
//                    ret = null;
//                    Log.e(TAG, "InterruptedException :" + e);
//                    break;
//                }
//                File flagFile = new File(PROGRESS_FLAG_FILE);
//                if (!flagFile.exists()) {
//                    whileFlag = false;
//                }
//                flagFile = null;
//            }
//        } catch (Exception e) {
//            ret = null;
//        } finally {
//            if (null != outPath) {
//                try {
//                    outPath.flush();
//                    outPath.close();
//                } catch (IOException e) {
//                }
//            }
//            if (null != outPackage) {
//                try {
//                    outPackage.flush();
//                    outPackage.close();
//                } catch (IOException e) {
//                }
//            }
//        }
//        return ret;
//    }
//    //================================主要操作程式碼======================================================== end
//
//    
// 
//    @Override
//    public boolean onCreate() {
//        return true;
//    }
//
//    @Override
//    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,String sortOrder) {
//        return   null ;
//    }
//
//    @Override
//    public int delete(Uri uri, String selection, String[] selectionArgs) {
//        return 0;
//    }
//
//    @Override
//    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
//        return 0;
//    }
//
//    @Override
//    public String getType(Uri uri) {
//        return null;
//    }
//}

 

相關文章