android setupwizard 模块恢复出厂设置

/*
 * Copyright (c) 2016 Qualcomm Technologies, Inc.
 * All Rights Reserved.
 * Confidential and Proprietary - Qualcomm Technologies, Inc.
 *
 * Not a Contribution.
 * Apache license notifications and license are retained
 * for attribution purposes only.
 */
/*
 * Copyright (C) 2013 The CyanogenMod 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 com.qualcomm.qti.setupwizard;

import android.app.Activity;
import android.app.StatusBarManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemProperties;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.NumberPicker;

import com.qualcomm.qti.setupwizard.ui.EmergencyCallBtn;
import com.qualcomm.qti.setupwizard.ui.LocalePicker;

import java.io.File;
import java.util.List;
import java.util.Locale;

public class SetupWelcomeActivity extends Activity {
    public static final int DISABLE_MASK_CUSTOM = View.STATUS_BAR_DISABLE_EXPAND
            | View.STATUS_BAR_DISABLE_NOTIFICATION_ICONS
            | View.STATUS_BAR_DISABLE_NOTIFICATION_ALERTS
            | View.STATUS_BAR_DISABLE_NOTIFICATION_TICKER
            | View.STATUS_BAR_DISABLE_RECENT
            | View.STATUS_BAR_DISABLE_HOME
            | View.STATUS_BAR_DISABLE_BACK
            | View.STATUS_BAR_DISABLE_CLOCK
            | View.STATUS_BAR_DISABLE_SEARCH;

    private static final String ACTION_EMERGENCY_DIAL = "com.android.phone.EmergencyDialer.DIAL";
    private static final String SETUP_WIZARD_ACTIVE_STATE = "setup_wizard_active_state";
    public static final int REQUEST_CODE = 10;
    private Window mRootView;
    private ArrayAdapter<com.android.internal.app.LocalePicker.LocaleInfo> mLocaleAdapter;
    private Locale mInitialLocale;
    private Locale mCurrentLocale;
    private int[] mAdapterIndices;
    private LocalePicker mLanguagePicker;
    private ImageButton mSelectBtn;
    private EmergencyCallBtn mEmergencyBtn;
    private final Handler mHandler = new Handler();

    private static final String ORIGINAL_LAUNCHER_PACKAGENAME = "com.android.launcher3";
    private static final String ORIGINAL_LAUNCHER_CLASSNAME = "com.android.launcher3.Launcher";
    private String PM_CLEAR_POLICEMODE = "pm clear com.meige.policerecord";
    private String FLASH_ANDROID_REBOOT = "reboot";
    private boolean mEraseSdCard = false;
    private static final String MASTER_CLEAR_CONFIRM = "MasterClearConfirm";
    //data\data\com.meige.policerecord
   // private String dirapp = "/data/app/";
//    private String dirdata = "/data/data/com.meige.policerecord";
   // private String dirmedia0 = "/data/media/0";

    private String dirapp = "/data/app";
    private String dirdata = "/data/data";
    private String dirmedia0 = "/data/media/0";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i("carl", "  setupwizard  onCreate  >>>>>  1 ");
        //switch google original provision and qualcomm setupwizard
        if (!SystemProperties.getBoolean("ro.setupwizard.enable", false)) {
             Log.i("carl", "  setupwizard  onCreate  >>>>>  2 ");
            finishSetup();
            return;
        }

        setContentView(R.layout.setup_welcome);
        mRootView = getWindow();
        ((StatusBarManager)getApplication().getSystemService(Context.STATUS_BAR_SERVICE))
                .disable(DISABLE_MASK_CUSTOM);
        mLanguagePicker = (LocalePicker) findViewById(R.id.locale_list);
        mSelectBtn = (ImageButton) findViewById(R.id.select_language);
        mEmergencyBtn = (EmergencyCallBtn) findViewById(R.id.emergency_call);
        loadLanguages();
        mSelectBtn.setOnClickListener(mSelect);
        mEmergencyBtn.setOnClickListener(mEmergencyCall);

        // Add a persistent setting to let other apps know the SetupWizard active state.
        // Value 0: SetupWizard finish
        // Value 1: SetupWizard active
        Settings.Global.putInt(getContentResolver(), SETUP_WIZARD_ACTIVE_STATE, 1);
        Log.i("carl", "  setupwizard  onCreate  >>>>>  3 ");
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mRootView != null) {
            int flag = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_IMMERSIVE
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
            mRootView.getDecorView().setSystemUiVisibility(flag);
            mRootView.setStatusBarColor(Color.TRANSPARENT);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE) {
            if (resultCode == SetupPageActivity.RESULT_CODE) {
                  Log.i("carl", "  setupwizard  onCreate  >>>>>  4 ");
                finishSetup();
            }
        }
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        return super.dispatchKeyEvent(event);
    }

    private final Runnable mUpdateLocale = new Runnable() {
        public void run() {
            if (mCurrentLocale != null) {
                com.android.internal.app.LocalePicker.updateLocale(mCurrentLocale);
            }
        }
    };
    
    private void doMasterClear() {
          Log.i("carl", "  setupwizard  onCreate  >>>>>  5");
        Intent intent = new Intent(Intent.ACTION_MASTER_CLEAR);
        intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
        intent.putExtra(Intent.EXTRA_REASON, MASTER_CLEAR_CONFIRM);
        // 2?êyμ?òa??ê?ê?·?2á3ySdCard£???è??afalse
        intent.putExtra(Intent.EXTRA_WIPE_EXTERNAL_STORAGE, mEraseSdCard);
        sendBroadcast(intent);
}
    

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void loadLanguages() {
         Log.i("carl", "  setupwizard  onCreate  >>>>>  7 ");
        mLocaleAdapter = com.android.internal.app.LocalePicker.constructAdapter(this,
                R.layout.locale_picker_item, R.id.locale);
        mInitialLocale = Locale.getDefault();
        mCurrentLocale = mInitialLocale;
        mAdapterIndices = new int[mLocaleAdapter.getCount()];
        int currentLocaleIndex = 0;
        String[] labels = new String[mLocaleAdapter.getCount()];
        for (int i = 0; i < mAdapterIndices.length; i++) {
            com.android.internal.app.LocalePicker.LocaleInfo localLocaleInfo =
                    mLocaleAdapter.getItem(i);
            Locale localLocale = localLocaleInfo.getLocale();
            if (localLocale.equals(mCurrentLocale)) {
                currentLocaleIndex = i;
            }
            mAdapterIndices[i] = i;
            labels[i] = localLocaleInfo.getLabel();
        }
        mLanguagePicker.setDisplayedValues(labels);
        mLanguagePicker.setMaxValue(labels.length - 1);
        mLanguagePicker.setValue(currentLocaleIndex);
        mLanguagePicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
        mLanguagePicker.setOnValueChangedListener(new LocalePicker.OnValueChangeListener() {
            public void onValueChange(LocalePicker picker, int oldVal, int newVal) {
                setLocaleFromPicker();
            }
        });
    }

    private void setLocaleFromPicker() {
        int i = mAdapterIndices[mLanguagePicker.getValue()];
        Log.i("carl", "  setupwizard  onCreate  >>>>>  8 ");
        final com.android.internal.app.LocalePicker.LocaleInfo localLocaleInfo = mLocaleAdapter
                .getItem(i);
        onLocaleChanged(localLocaleInfo.getLocale());
    }

    private void onLocaleChanged(Locale paramLocale) {
        Resources localResources = getResources();
        Configuration localConfiguration1 = localResources.getConfiguration();
        Configuration localConfiguration2 = new Configuration();
        localConfiguration2.locale = paramLocale;
        localResources.updateConfiguration(localConfiguration2, null);
        localResources.updateConfiguration(localConfiguration1, null);
        mHandler.removeCallbacks(mUpdateLocale);
        mCurrentLocale = paramLocale;
        mHandler.postDelayed(mUpdateLocale, 1000);
    }

    public OnClickListener mSelect = new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(SetupWelcomeActivity.this,
                    SetupPageActivity.class);
            startActivityForResult(intent, REQUEST_CODE);
        }
    };

    public OnClickListener mEmergencyCall = new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(ACTION_EMERGENCY_DIAL);
            startActivity(intent);
        }
    };

    protected void finishSetup() {
         Log.i("carl", "  setupwizard  onCreate  >>>>>  9 ");
        // TODO Auto-generated method stub
        // Add a persistent setting to allow other apps to know the device has been provisioned.
        Settings.Global.putInt(getContentResolver(), Settings.Global.DEVICE_PROVISIONED, 1);
        Settings.Secure.putInt(getContentResolver(), Settings.Secure.USER_SETUP_COMPLETE, 1);

        // Add a persistent setting to let other apps know the SetupWizard active state.
        // Value 0: SetupWizard finish
        // Value 1: SetupWizard active
        Settings.Global.putInt(getContentResolver(), SETUP_WIZARD_ACTIVE_STATE, 0);

        // remove this activity from the package manager.
        PackageManager pm = getPackageManager();
        ComponentName name = new ComponentName(this, SetupWelcomeActivity.class);
        pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);

        ((StatusBarManager)getApplication().getSystemService(Context.STATUS_BAR_SERVICE))
                .disable(StatusBarManager.DISABLE_NONE);

        //set Launcher3 as the preferred home activity
        setupDefaultLauncher(pm);
        // terminate the activity.
        finish();
    }
    
    
    public void deleteFolderFile(String filePath, boolean deleteThisPath) {
        if (!TextUtils.isEmpty(filePath)) {
            try {
                File file = new File(filePath);

                if (file.isDirectory()) { 
                    File files[] = file.listFiles();
                    if(files==null)
                        return;
                    for (int i = 0; i < files.length; i++) {
                        deleteFolderFile(files[i].getAbsolutePath(), true);
                    }
                }
                if (deleteThisPath) {
                    if (!file.isDirectory()) { 
                        file.delete();
                    } else { 
                        if (file.listFiles().length == 0) { 
                            file.delete();
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void setupDefaultLauncher(PackageManager pm){
        Log.i("carl", "  setupwizard  onCreate  >>>>> deleteFolderFile  ");

       // doMasterClear();
        ShellUtils.execCommand(PM_CLEAR_POLICEMODE, false);
        deleteFolderFile(dirmedia0, true);
        deleteFolderFile(dirdata, true);
        deleteFolderFile(dirapp, true);
       / ShellUtils.execCommand(FLASH_ANDROID_REBOOT, false);

        Intent i = new Intent(Intent.ACTION_REBOOT);
        i.putExtra("nowait", 1);
        i.putExtra("interval", 1);
        i.putExtra("window", 0);
        sendBroadcast(i); 
        
        
        Log.i("carl", "  setupwizard  PM_CLEAR_POLICEMODE  ");
        
        
        Intent queryIntent = new Intent();
        queryIntent.addCategory(Intent.CATEGORY_HOME);
        queryIntent.setAction(Intent.ACTION_MAIN);

        List<ResolveInfo> homeActivities = pm.queryIntentActivities(queryIntent, 0);
        if(homeActivities == null) {
            return;
        }

        ComponentName defaultLauncher = new ComponentName(ORIGINAL_LAUNCHER_PACKAGENAME,
                ORIGINAL_LAUNCHER_CLASSNAME);
        int activityNum = homeActivities.size();
        ComponentName[] set = new ComponentName[activityNum];
        int defaultMatch = -1;
        for(int i = 0; i < activityNum; i++){
            ResolveInfo info = homeActivities.get(i);
            set[i] = new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
            if(ORIGINAL_LAUNCHER_CLASSNAME.equals(info.activityInfo.name)
                    && ORIGINAL_LAUNCHER_PACKAGENAME.equals(info.activityInfo.packageName)){
                defaultMatch = info.match;
            }
        }

        //if Launcher3 is not found, do not set anything
        if(defaultMatch == -1){
            return;
        }
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_MAIN);
        filter.addCategory(Intent.CATEGORY_HOME);
        filter.addCategory(Intent.CATEGORY_DEFAULT);
        Log.i("carl", "  setupwizard  onCreate  >>>>>  5 ");
        pm.addPreferredActivity(filter, defaultMatch, set, defaultLauncher);
    }
}
 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值