Search in sources :

Example 81 with PackageManager

use of android.content.pm.PackageManager in project leakcanary by square.

the class LeakCanaryInternals method isInServiceProcess.

public static boolean isInServiceProcess(Context context, Class<? extends Service> serviceClass) {
    PackageManager packageManager = context.getPackageManager();
    PackageInfo packageInfo;
    try {
        packageInfo = packageManager.getPackageInfo(context.getPackageName(), GET_SERVICES);
    } catch (Exception e) {
        CanaryLog.d(e, "Could not get package info for %s", context.getPackageName());
        return false;
    }
    String mainProcess = packageInfo.applicationInfo.processName;
    ComponentName component = new ComponentName(context, serviceClass);
    ServiceInfo serviceInfo;
    try {
        serviceInfo = packageManager.getServiceInfo(component, 0);
    } catch (PackageManager.NameNotFoundException ignored) {
        // Service is disabled.
        return false;
    }
    if (serviceInfo.processName.equals(mainProcess)) {
        CanaryLog.d("Did not expect service %s to run in main process %s", serviceClass, mainProcess);
        // Technically we are in the service process, but we're not in the service dedicated process.
        return false;
    }
    int myPid = android.os.Process.myPid();
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    ActivityManager.RunningAppProcessInfo myProcess = null;
    List<ActivityManager.RunningAppProcessInfo> runningProcesses = activityManager.getRunningAppProcesses();
    if (runningProcesses != null) {
        for (ActivityManager.RunningAppProcessInfo process : runningProcesses) {
            if (process.pid == myPid) {
                myProcess = process;
                break;
            }
        }
    }
    if (myProcess == null) {
        CanaryLog.d("Could not find running process for %d", myPid);
        return false;
    }
    return myProcess.processName.equals(serviceInfo.processName);
}
Also used : ServiceInfo(android.content.pm.ServiceInfo) PackageManager(android.content.pm.PackageManager) PackageInfo(android.content.pm.PackageInfo) ComponentName(android.content.ComponentName) ActivityManager(android.app.ActivityManager)

Example 82 with PackageManager

use of android.content.pm.PackageManager in project SearchView by lapism.

the class SearchView method isVoiceAvailable.

private boolean isVoiceAvailable() {
    if (isInEditMode()) {
        return true;
    }
    PackageManager pm = getContext().getPackageManager();
    List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
    return activities.size() != 0;
}
Also used : ResolveInfo(android.content.pm.ResolveInfo) PackageManager(android.content.pm.PackageManager) Intent(android.content.Intent) RecognizerIntent(android.speech.RecognizerIntent)

Example 83 with PackageManager

use of android.content.pm.PackageManager in project android-common by litesuits.

the class SilentInstaller method isSystemApplication.

/**
     * /**
     * whether packageName is system application
     */
public boolean isSystemApplication(Context context) {
    PackageManager packageManager = context.getPackageManager();
    String packageName = context.getPackageName();
    if (packageManager == null || packageName == null || packageName.length() == 0) {
        return false;
    }
    try {
        ApplicationInfo app = packageManager.getApplicationInfo(packageName, 0);
        return (app != null && (app.flags & ApplicationInfo.FLAG_SYSTEM) > 0);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}
Also used : PackageManager(android.content.pm.PackageManager) ApplicationInfo(android.content.pm.ApplicationInfo)

Example 84 with PackageManager

use of android.content.pm.PackageManager in project robolectric by robolectric.

the class ParallelUniverse method setUpApplicationState.

@Override
public void setUpApplicationState(Method method, TestLifecycle testLifecycle, AndroidManifest appManifest, Config config, ResourceTable compileTimeResourceTable, ResourceTable appResourceTable, ResourceTable systemResourceTable) {
    ReflectionHelpers.setStaticField(RuntimeEnvironment.class, "apiLevel", sdkConfig.getApiLevel());
    RuntimeEnvironment.application = null;
    RuntimeEnvironment.setMasterScheduler(new Scheduler());
    RuntimeEnvironment.setMainThread(Thread.currentThread());
    DefaultPackageManager packageManager = new DefaultPackageManager();
    RuntimeEnvironment.setRobolectricPackageManager(packageManager);
    RuntimeEnvironment.setCompileTimeResourceTable(compileTimeResourceTable);
    RuntimeEnvironment.setAppResourceTable(appResourceTable);
    RuntimeEnvironment.setSystemResourceTable(systemResourceTable);
    initializeAppManifest(appManifest, appResourceTable, packageManager);
    packageManager.setDependencies(appManifest, appResourceTable);
    if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
        Security.insertProviderAt(new BouncyCastleProvider(), 1);
    }
    String qualifiers = Qualifiers.addPlatformVersion(config.qualifiers(), sdkConfig.getApiLevel());
    qualifiers = Qualifiers.addSmallestScreenWidth(qualifiers, 320);
    qualifiers = Qualifiers.addScreenWidth(qualifiers, 320);
    Resources systemResources = Resources.getSystem();
    Configuration configuration = systemResources.getConfiguration();
    configuration.smallestScreenWidthDp = Qualifiers.getSmallestScreenWidth(qualifiers);
    configuration.screenWidthDp = Qualifiers.getScreenWidth(qualifiers);
    systemResources.updateConfiguration(configuration, systemResources.getDisplayMetrics());
    RuntimeEnvironment.setQualifiers(qualifiers);
    Class<?> contextImplClass = ReflectionHelpers.loadClass(getClass().getClassLoader(), shadowsAdapter.getShadowContextImplClassName());
    Class<?> activityThreadClass = ReflectionHelpers.loadClass(getClass().getClassLoader(), shadowsAdapter.getShadowActivityThreadClassName());
    // Looper needs to be prepared before the activity thread is created
    if (Looper.myLooper() == null) {
        Looper.prepareMainLooper();
    }
    ShadowLooper.getShadowMainLooper().resetScheduler();
    Object activityThread = ReflectionHelpers.newInstance(activityThreadClass);
    RuntimeEnvironment.setActivityThread(activityThread);
    ReflectionHelpers.setField(activityThread, "mInstrumentation", new RoboInstrumentation());
    ReflectionHelpers.setField(activityThread, "mCompatConfiguration", configuration);
    Context systemContextImpl = ReflectionHelpers.callStaticMethod(contextImplClass, "createSystemContext", ClassParameter.from(activityThreadClass, activityThread));
    final Application application = (Application) testLifecycle.createApplication(method, appManifest, config);
    RuntimeEnvironment.application = application;
    if (application != null) {
        shadowsAdapter.bind(application, appManifest);
        ApplicationInfo applicationInfo;
        try {
            applicationInfo = packageManager.getApplicationInfo(appManifest.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            throw new RuntimeException(e);
        }
        Class<?> compatibilityInfoClass = ReflectionHelpers.loadClass(getClass().getClassLoader(), "android.content.res.CompatibilityInfo");
        LoadedApk loadedApk = ReflectionHelpers.callInstanceMethod(activityThread, "getPackageInfo", ClassParameter.from(ApplicationInfo.class, applicationInfo), ClassParameter.from(compatibilityInfoClass, null), ClassParameter.from(int.class, Context.CONTEXT_INCLUDE_CODE));
        try {
            Context contextImpl = systemContextImpl.createPackageContext(applicationInfo.packageName, Context.CONTEXT_INCLUDE_CODE);
            ReflectionHelpers.setField(activityThreadClass, activityThread, "mInitialApplication", application);
            ApplicationTestUtil.attach(application, contextImpl);
        } catch (PackageManager.NameNotFoundException e) {
            throw new RuntimeException(e);
        }
        Resources appResources = application.getResources();
        ReflectionHelpers.setField(loadedApk, "mResources", appResources);
        ReflectionHelpers.setField(loadedApk, "mApplication", application);
        appResources.updateConfiguration(configuration, appResources.getDisplayMetrics());
        application.onCreate();
    }
}
Also used : Context(android.content.Context) Configuration(android.content.res.Configuration) LoadedApk(android.app.LoadedApk) Scheduler(org.robolectric.util.Scheduler) RoboInstrumentation(org.robolectric.android.fakes.RoboInstrumentation) DefaultPackageManager(org.robolectric.res.builder.DefaultPackageManager) ApplicationInfo(android.content.pm.ApplicationInfo) PackageManager(android.content.pm.PackageManager) DefaultPackageManager(org.robolectric.res.builder.DefaultPackageManager) Resources(android.content.res.Resources) Application(android.app.Application) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider)

Example 85 with PackageManager

use of android.content.pm.PackageManager in project ShortcutBadger by leolin310148.

the class BroadcastHelper method canResolveBroadcast.

public static boolean canResolveBroadcast(Context context, Intent intent) {
    PackageManager packageManager = context.getPackageManager();
    List<ResolveInfo> receivers = packageManager.queryBroadcastReceivers(intent, 0);
    return receivers != null && receivers.size() > 0;
}
Also used : ResolveInfo(android.content.pm.ResolveInfo) PackageManager(android.content.pm.PackageManager)

Aggregations

PackageManager (android.content.pm.PackageManager)1482 Intent (android.content.Intent)505 ResolveInfo (android.content.pm.ResolveInfo)460 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)353 PackageInfo (android.content.pm.PackageInfo)270 ApplicationInfo (android.content.pm.ApplicationInfo)253 ComponentName (android.content.ComponentName)241 ArrayList (java.util.ArrayList)158 ActivityInfo (android.content.pm.ActivityInfo)140 IOException (java.io.IOException)127 RemoteException (android.os.RemoteException)105 Drawable (android.graphics.drawable.Drawable)94 IPackageManager (android.content.pm.IPackageManager)93 Resources (android.content.res.Resources)91 PendingIntent (android.app.PendingIntent)75 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)69 Context (android.content.Context)68 Bundle (android.os.Bundle)60 HashMap (java.util.HashMap)55 ServiceInfo (android.content.pm.ServiceInfo)48