Java Code Examples for android.content.pm.PackageManager#COMPONENT_ENABLED_STATE_DEFAULT

The following examples show how to use android.content.pm.PackageManager#COMPONENT_ENABLED_STATE_DEFAULT . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
/**
 * Detect the current "Autostart if card is detected" state and set
 * the checkbox accordingly.
 */
private void detectAutostartIfCardDetectedState() {
    int enabledSetting = mPackageManager.getComponentEnabledSetting(
            mComponentName);
    switch (enabledSetting) {
        case PackageManager.COMPONENT_ENABLED_STATE_ENABLED:
        case PackageManager.COMPONENT_ENABLED_STATE_DEFAULT:
            mPrefAutostartIfCardDetected.setChecked(true);
            break;
        case PackageManager.COMPONENT_ENABLED_STATE_DISABLED:
            mPrefAutostartIfCardDetected.setChecked(false);
            break;
        default:
            break;
    }
}
 
Example 2
Source Project: Nimingban   File: AbsActivity.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void onReceive(final int id, final Object obj) {
    if (id == Constants.MESSENGER_ID_CHANGE_THEME) {
        String cls = getComponentName().getClassName();
        // Change icon will disable this activity, recreate will crash
        // So we need to find the enabled activity
        if (Utilities.contain(Settings.ICON_ACTIVITY_ARRAY, cls)) {
            ComponentName c = new ComponentName(this, cls);
            PackageManager p = getPackageManager();
            int state = p.getComponentEnabledSetting(c);

            if ((state == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT &&
                    !cls.equals(Settings.getDefaultIconActivity())) |
                    state == PackageManager.COMPONENT_ENABLED_STATE_DISABLED) {
                finish();
                Intent intent = new Intent();
                intent.setClassName(this, Settings.getCurrentIconActivity());
                startActivity(intent);
                return;
            }
        }

        recreate();
    }
}
 
Example 3
public boolean isFrozen(Context context) {
	if (mFrozen == null) {
		PackageManager pm = context.getPackageManager();
		boolean enabled = false;
		for (ApplicationInfo appInfo : mMapAppInfo.values())
			try {
				int setting = pm.getApplicationEnabledSetting(appInfo.packageName);
				enabled = (enabled || setting == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
				enabled = (enabled || setting == PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
				if (enabled)
					break;
			} catch (IllegalArgumentException ignored) {
			}
		mFrozen = !enabled;
	}
	return mFrozen;
}
 
Example 4
private Rule(PackageInfo info, boolean wifi_blocked, boolean other_blocked, boolean changed, Context context) {
    PackageManager pm = context.getPackageManager();
    this.info = info;
    this.name = info.applicationInfo.loadLabel(pm).toString();
    this.system = ((info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0);

    int setting = pm.getApplicationEnabledSetting(info.packageName);
    if (setting == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT)
        this.disabled = !info.applicationInfo.enabled;
    else
        this.disabled = (setting != PackageManager.COMPONENT_ENABLED_STATE_ENABLED);

    this.wifi_blocked = wifi_blocked;
    this.other_blocked = other_blocked;
    this.changed = changed;
}
 
Example 5
Source Project: XQuickEnergy   File: MainActivity.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
 switch(item.getItemId())
 {
  case 1:
   int state = item.isChecked() ? PackageManager.COMPONENT_ENABLED_STATE_DEFAULT: PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
   getPackageManager()
    .setComponentEnabledSetting(new ComponentName(this, getClass().getCanonicalName() + "Alias"), state, PackageManager.DONT_KILL_APP);
   item.setChecked(!item.isChecked());
   break;

  case 2:
   if(FileUtils.copyTo(FileUtils.getStatisticsFile(), FileUtils.getExportedStatisticsFile()))
    Toast.makeText(this, "Export success", 0).show();
   break;

  case 3:
   if(FileUtils.copyTo(FileUtils.getExportedStatisticsFile(), FileUtils.getStatisticsFile()))
   {
    tv_statistics.setText(Statistics.getText());
    Toast.makeText(this, "Import success", 0).show();
   }
   break;

  case 4:
   startActivity(new Intent(this, SettingsActivity.class));
   break;
 }
 return super.onOptionsItemSelected(item);
}
 
Example 6
Source Project: NetGuard   File: Util.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static boolean isEnabled(PackageInfo info, Context context) {
    int setting;
    try {
        PackageManager pm = context.getPackageManager();
        setting = pm.getApplicationEnabledSetting(info.packageName);
    } catch (IllegalArgumentException ex) {
        setting = PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
        Log.w(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
    }
    if (setting == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT)
        return info.applicationInfo.enabled;
    else
        return (setting == PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
}
 
Example 7
@Override
public int getApplicationEnabledSetting(String packageName) {
    try {
        return mPM.getApplicationEnabledSetting(packageName, mContext.getUserId());
    } catch (RemoteException e) {
        // Should never happen!
    }
    return PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
}
 
Example 8
@Override
public int getApplicationEnabledSetting(String packageName) {
    try {
        return mPM.getApplicationEnabledSetting(packageName);
    } catch (RemoteException e) {
        // Should never happen!
    }
    return PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
}
 
Example 9
@Override
public int getComponentEnabledSetting(ComponentName componentName) {
    try {
        return mPM.getComponentEnabledSetting(componentName, mContext.getUserId());
    } catch (RemoteException e) {
        // Should never happen!
    }
    return PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
}
 
Example 10
Source Project: kcanotify   File: Util.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static boolean isEnabled(PackageInfo info, Context context) {
    int setting;
    try {
        PackageManager pm = context.getPackageManager();
        setting = pm.getApplicationEnabledSetting(info.packageName);
    } catch (IllegalArgumentException ex) {
        setting = PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
        Log.w(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
    }
    if (setting == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT)
        return info.applicationInfo.enabled;
    else
        return (setting == PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
}
 
Example 11
/**
 * Disables BluetoothOppLauncherActivity component, so the Bluetooth sharing option is not
 * offered to the user if Bluetooth or sharing is disallowed. Puts the component to its default
 * state if Bluetooth is not disallowed.
 *
 * @param userId user to disable bluetooth sharing for.
 * @param bluetoothSharingDisallowed whether bluetooth sharing is disallowed.
 */
private void updateOppLauncherComponentState(int userId, boolean bluetoothSharingDisallowed) {
    final ComponentName oppLauncherComponent = new ComponentName("com.android.bluetooth",
            "com.android.bluetooth.opp.BluetoothOppLauncherActivity");
    final int newState =
            bluetoothSharingDisallowed ? PackageManager.COMPONENT_ENABLED_STATE_DISABLED
                    : PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
    try {
        final IPackageManager imp = AppGlobals.getPackageManager();
        imp.setComponentEnabledSetting(oppLauncherComponent, newState,
                PackageManager.DONT_KILL_APP, userId);
    } catch (Exception e) {
        // The component was not found, do nothing.
    }
}
 
Example 12
private boolean getPauseForCall() {
    switch (getActivity().getPackageManager().getComponentEnabledSetting(PHONE_STATE_RECEIVER)) {
        case PackageManager.COMPONENT_ENABLED_STATE_DEFAULT:
        case PackageManager.COMPONENT_ENABLED_STATE_ENABLED:
            return true;
        default:
            return false;
    }
}
 
Example 13
/**
 * This method hides the given class
 *
 * @param context the context to use for this operation
 * @param toHide  the class to hide
 */
public static void hideClass(Context context, Class<?> toHide, int flag) {
	PackageManager pm = context.getPackageManager();
	ComponentName homeName = new ComponentName(context, toHide);
	if (pm != null
			&& pm.getComponentEnabledSetting(homeName) == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
		pm.setComponentEnabledSetting(homeName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, flag);
	}
}
 
Example 14
Source Project: InviZible   File: Util.java    License: GNU General Public License v3.0 5 votes vote down vote up
static boolean isEnabled(PackageInfo info, Context context) {
    int setting;
    try {
        PackageManager pm = context.getPackageManager();
        setting = pm.getApplicationEnabledSetting(info.packageName);
    } catch (IllegalArgumentException ex) {
        setting = PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
        Log.w(LOG_TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
    }
    if (setting == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT)
        return info.applicationInfo.enabled;
    else
        return (setting == PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
}
 
Example 15
public String icoStatus() {
    if (getPackageManager().getComponentEnabledSetting(getAliseComponentName()) == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
        return "点击隐藏应用图标";
    } else {
        return "点击显示应用图标";
    }
}
 
Example 16
Source Project: GPT   File: PackageManagerWorker.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * getComponentEnabledSetting
 *
 * @param componentName ComponentName
 * @param userId        userId
 * @return ComponentEnabledSetting
 */
public int getComponentEnabledSetting(ComponentName componentName, int userId) {
    String packageName = componentName.getPackageName();
    if (isPlugin(packageName)) {
        return PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
    } else {
        if (userId == INVALID_USER_ID) {
            return mTarget.getComponentEnabledSetting(componentName);
        } else {
            return mTarget.getComponentEnabledSetting(componentName, userId);
        }
    }
}
 
Example 17
Source Project: JobSchedulerCompat   File: DeviceTestUtils.java    License: MIT License 5 votes vote down vote up
@SuppressWarnings("deprecation")
public static boolean isComponentEnabled(PackageManager manager, ComponentName component) {
    switch (manager.getComponentEnabledSetting(component)) {
        case PackageManager.COMPONENT_ENABLED_STATE_DISABLED:
            return false;
        case PackageManager.COMPONENT_ENABLED_STATE_ENABLED:
            return true;
        case PackageManager.COMPONENT_ENABLED_STATE_DEFAULT:
        default:
            try {
                PackageInfo packageInfo = manager.getPackageInfo(
                        component.getPackageName(),
                        PackageManager.GET_ACTIVITIES | PackageManager.GET_RECEIVERS | PackageManager.GET_SERVICES
                                | PackageManager.GET_PROVIDERS | PackageManager.GET_DISABLED_COMPONENTS);
                List<ComponentInfo> components = new ArrayList<>();
                if (packageInfo.activities != null) {
                    Collections.addAll(components, packageInfo.activities);
                }
                if (packageInfo.services != null) {
                    Collections.addAll(components, packageInfo.services);
                }
                if (packageInfo.providers != null) {
                    Collections.addAll(components, packageInfo.providers);
                }
                for (ComponentInfo componentInfo : components) {
                    if (componentInfo.name.equals(component.getClassName())) {
                        return componentInfo.isEnabled();
                    }
                }
                return false;
            } catch (PackageManager.NameNotFoundException e) {
                // the package isn't installed on the device
                return false;
            }
    }
}
 
Example 18
/**
 * Checks if component for provided class enabled before.
 *
 * @param context Current Context.
 * @param packageManager Current PackageManager.
 * @param clazz Class for check.
 * @return True if component was enabled before.
 */
public static boolean wasComponentEnabled(Context context, PackageManager packageManager,
    Class clazz) {
  if (clazz == null || context == null || packageManager == null) {
    return false;
  }
  int componentStatus = packageManager.getComponentEnabledSetting(new ComponentName(context,
      clazz));
  if (PackageManager.COMPONENT_ENABLED_STATE_DEFAULT == componentStatus ||
      PackageManager.COMPONENT_ENABLED_STATE_DISABLED == componentStatus) {
    return false;
  }
  return true;
}
 
Example 19
private boolean isValidActivity(Intent intent) {
    List<ResolveInfo> list = activity.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);

    if (list.size() > 0) {
        ActivityInfo ai = list.get(0).activityInfo;
        if (ai != null && (ai.permission==null || activity.checkCallingOrSelfPermission(ai.permission)==PackageManager.PERMISSION_GRANTED)) {
            int enabledflag = activity.getPackageManager().getComponentEnabledSetting(intent.getComponent());

            return ai.exported && (enabledflag == PackageManager.COMPONENT_ENABLED_STATE_ENABLED || enabledflag == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT && ai.enabled);
            //return ai.enabled && ai.exported;
        }
    }
    return false;
}
 
Example 20
Source Project: android-job   File: GcmAvailableHelper.java    License: Apache License 2.0 5 votes vote down vote up
private static void setServiceEnabled(Context context, boolean enabled) {
    try {
        PackageManager packageManager = context.getPackageManager();

        // use a string, the class object probably cannot be instantiated
        ComponentName component = new ComponentName(context, getPlatformGcmServiceClassName());

        int componentEnabled = packageManager.getComponentEnabledSetting(component);
        switch (componentEnabled) {
            case PackageManager.COMPONENT_ENABLED_STATE_ENABLED:
                if (!enabled) {
                    packageManager.setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
                    CAT.i("GCM service disabled");
                }
                break;

            case PackageManager.COMPONENT_ENABLED_STATE_DEFAULT: // default is disable
            case PackageManager.COMPONENT_ENABLED_STATE_DISABLED:
                if (enabled) {
                    packageManager.setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
                    CAT.i("GCM service enabled");
                }
                break;
            case PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED:
            case PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER:
                // do nothing
                break;
        }

    } catch (Throwable t) {
        // just in case, don't let the app crash with each restart
        if (BuildConfig.DEBUG) {
            CAT.e(t.getMessage());
        }
    }
}