Home | History | Annotate | Download | only in helpers
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.system.helpers;
     18 
     19 import android.app.Instrumentation;
     20 import android.content.pm.PackageManager;
     21 import android.os.SystemClock;
     22 import android.system.helpers.CommandsHelper;
     23 
     24 /**
     25  * Implement common helper methods for package management.
     26  * eg. Delete package data.
     27  */
     28 public class PackageHelper {
     29     public final int TIMEOUT = 500;
     30     public static PackageHelper sInstance = null;
     31     private Instrumentation mInstrumentation = null;
     32     private CommandsHelper cmdHelper = null;
     33     private PackageManager mPackageManager = null;
     34 
     35     public PackageHelper(Instrumentation instrumentation) {
     36         mInstrumentation = instrumentation;
     37         cmdHelper = CommandsHelper.getInstance(instrumentation);
     38         mPackageManager = instrumentation.getTargetContext().getPackageManager();
     39     }
     40 
     41     public static PackageHelper getInstance(Instrumentation instrumentation) {
     42         if (sInstance == null) {
     43             sInstance = new PackageHelper(instrumentation);
     44         }
     45         return sInstance;
     46     }
     47 
     48     /**
     49      * Deletes all data associated with the package.
     50      * @param packageName package name
     51      */
     52     public void cleanPackage(String packageName) {
     53         cmdHelper.executeShellCommand(String.format("pm clear %s", packageName));
     54         SystemClock.sleep(2 * TIMEOUT);
     55     }
     56 
     57     /**
     58      * Check if certain package is installed on the device.
     59      * @param packageName package name
     60      * @return true/false
     61      */
     62     public Boolean isPackageInstalled(String packageName) {
     63         try {
     64             mPackageManager.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
     65             return true;
     66         } catch (PackageManager.NameNotFoundException e) {
     67             return false;
     68         }
     69     }
     70 }
     71