1. 程式人生 > >如何禁止adb命令安裝apk及手動安裝apk

如何禁止adb命令安裝apk及手動安裝apk

1.禁止adb命令安裝apk

修改位置:frameworks/base/cmds/pm/src/com/android/commands/pm/Pm.java

    private int runInstall() {
        int installFlags = 0;
        int userId = UserHandle.USER_ALL;
        String installerPackageName = null;
        ...
   +    boolean allowInstall = false;
        ...
        while ((opt=nextOption()) != null) {
        ...
                    System.err.println("Error: must supply argument for --referrer");
                    return 1;
                }
   +        }else if (opt.equals("-a")){
   +    	    allowInstall = true;
            } else if (opt.equals("--abi")) {
        ...
            } else {
                System.err.println("Error: Unknown option: " + opt);
                return 1;
            }
        }
    +	if(!allowInstall){
    +       System.err.println("Error: Unknown apk");
    +       return 1;
    +	}
        ...
    }
    

當執行adb命令安裝apk時,系統會呼叫Pm.java中的runInstall方法,同時檢測user使用的opt引數,再走安裝流程(此處不再贅述具體安裝流程)。如果在這段程式碼中能提前return退出程式,那麼就無法執行安裝流程,此時增加一變數allowInstall,只有當user

使用-a引數(預設無-a命令安裝apk)才能成功安裝apk。否則終端將輸出"Error: Unknown apk"。至此,成功實現禁止adb命令安裝apk。

2.禁止手動安裝apk

所謂的手動安裝apk指的是將apk拷貝到T卡或者sdcard下進行手動安裝。該過程會呼叫系統的PackageInstaller模組。要實現禁止安裝apk,具體修改位置:packages/apps/PackageInstaller/src/com/android/packageinstaller/PackageInstallerActivity.java

===================================================================
--- packages/apps/PackageInstaller/src/com/android/packageinstaller/PackageInstallerActivity.java	(revision 1118)
+++ packages/apps/PackageInstaller/src/com/android/packageinstaller/PackageInstallerActivity.java	(revision 1119)
@@ -54,6 +54,7 @@
 
 import java.io.File;
 import java.util.List;
+import android.os.SystemProperties;
 /*
  * This activity is launched when a new application is installed via side loading
@@ -110,6 +111,7 @@
     private static final int DLG_INSTALL_ERROR = DLG_BASE + 4;
     private static final int DLG_ALLOW_SOURCE = DLG_BASE + 5;
     private static final int DLG_ADMIN_RESTRICTS_UNKNOWN_SOURCES = DLG_BASE + 6;
+    private static final int DLG_FORBID_APPS = DLG_BASE + 7;//for forbid install apk
 
     private void startInstallConfirm() {
         TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
@@ -333,6 +335,20 @@
                     })
                     .setOnCancelListener(this)
                     .create();
+        case DLG_FORBID_APPS:
+            return new AlertDialog.Builder(this)
+                    .setTitle(R.string.forbid_apps_dlg_title)
+                    .setMessage(R.string.forbid_apps_dlg_text)
+                    .setPositiveButton(R.string.ok,new DialogInterface.OnClickListener(){
+                        @Override
+                        public void onClick(DialogInterface dialogInterface, int i) {
+                            finish();
+                        }
+                    })
+                    .setOnCancelListener(this)
+                    .create();
        }
        return null;
    }
@@ -481,6 +497,16 @@
         mInstallFlowAnalytics.setInstallRequestFromUnknownSource(requestFromUnknownSource);
         mInstallFlowAnalytics.setVerifyAppsEnabled(isVerifyAppsEnabled());
         mInstallFlowAnalytics.setAppVerifierInstalled(isAppVerifierInstalled());
+        if (mPackageURI == null) {
+            Log.w(TAG, "invalid URI");
+            setPmResult(PackageManager.INSTALL_FAILED_INVALID_URI);
+            mInstallFlowAnalytics.setFlowFinished(
+                    InstallFlowAnalytics.RESULT_FAILED_UNSUPPORTED_SCHEME);
+            finish();
+            return;
+        }
         mInstallFlowAnalytics.setPackageUri(mPackageURI.toString());
 
         final String scheme = mPackageURI.getScheme();
@@ -564,7 +590,18 @@
             mInstallFlowAnalytics.setFlowFinished(
                     InstallFlowAnalytics.RESULT_BLOCKED_BY_UNKNOWN_SOURCES_SETTING);
         } else {
-            initiateInstall();
+            if (!SystemProperties.getBoolean("ro.product.installApk", true)){
+                showDialogInner(DLG_FORBID_APPS);
+                mInstallFlowAnalytics.setFlowFinished(
+                        				 InstallFlowAnalytics.RESULT_BLOCKED_BY_UNKNOWN_SOURCES_SETTING);
+                return;
+
+            }else{
+                initiateInstall();
+            }
         }
     }