Search in sources :

Example 1 with AppSwitchPreference

use of com.android.settingslib.widget.AppSwitchPreference in project android_packages_apps_Settings by omnirom.

the class ManagedServiceSettings method updateList.

private void updateList(List<ServiceInfo> services) {
    UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
    final int managedProfileId = Utils.getManagedProfileId(um, UserHandle.myUserId());
    final PreferenceScreen screen = getPreferenceScreen();
    screen.removeAll();
    services.sort(new PackageItemInfo.DisplayNameComparator(mPm));
    for (ServiceInfo service : services) {
        final ComponentName cn = new ComponentName(service.packageName, service.name);
        CharSequence title = null;
        try {
            title = mPm.getApplicationInfoAsUser(service.packageName, 0, UserHandle.myUserId()).loadLabel(mPm);
        } catch (PackageManager.NameNotFoundException e) {
            // unlikely, as we are iterating over live services.
            Log.e(TAG, "can't find package name", e);
        }
        final CharSequence finalTitle = title;
        final String summary = service.loadLabel(mPm).toString();
        final SwitchPreference pref = new AppSwitchPreference(getPrefContext());
        pref.setPersistent(false);
        pref.setIcon(mIconDrawableFactory.getBadgedIcon(service, service.applicationInfo, UserHandle.getUserId(service.applicationInfo.uid)));
        if (title != null && !title.equals(summary)) {
            pref.setTitle(title);
            pref.setSummary(summary);
        } else {
            pref.setTitle(summary);
        }
        pref.setKey(cn.flattenToString());
        pref.setChecked(isServiceEnabled(cn));
        if (managedProfileId != UserHandle.USER_NULL && !mDpm.isNotificationListenerServicePermitted(service.packageName, managedProfileId)) {
            pref.setSummary(R.string.work_profile_notification_access_blocked_summary);
        }
        pref.setOnPreferenceChangeListener((preference, newValue) -> {
            final boolean enable = (boolean) newValue;
            if (finalTitle != null) {
                return setEnabled(cn, finalTitle.toString(), enable);
            } else {
                return setEnabled(cn, null, enable);
            }
        });
        pref.setKey(cn.flattenToString());
        screen.addPreference(pref);
    }
    highlightPreferenceIfNeeded();
}
Also used : AppSwitchPreference(com.android.settingslib.widget.AppSwitchPreference) PreferenceScreen(androidx.preference.PreferenceScreen) AppSwitchPreference(com.android.settingslib.widget.AppSwitchPreference) SwitchPreference(androidx.preference.SwitchPreference) ServiceInfo(android.content.pm.ServiceInfo) PackageManager(android.content.pm.PackageManager) UserManager(android.os.UserManager) PackageItemInfo(android.content.pm.PackageItemInfo) ComponentName(android.content.ComponentName)

Example 2 with AppSwitchPreference

use of com.android.settingslib.widget.AppSwitchPreference in project android_packages_apps_Settings by omnirom.

the class DeviceAdminListPreferenceController method refreshUI.

private void refreshUI() {
    if (mPreferenceGroup == null) {
        return;
    }
    if (mFooterPreference != null) {
        mFooterPreference.setVisible(mAdmins.isEmpty());
    }
    final Map<String, AppSwitchPreference> preferenceCache = new ArrayMap<>();
    final Context prefContext = mPreferenceGroup.getContext();
    final int childrenCount = mPreferenceGroup.getPreferenceCount();
    for (int i = 0; i < childrenCount; i++) {
        final Preference pref = mPreferenceGroup.getPreference(i);
        if (!(pref instanceof AppSwitchPreference)) {
            continue;
        }
        final AppSwitchPreference appSwitch = (AppSwitchPreference) pref;
        preferenceCache.put(appSwitch.getKey(), appSwitch);
    }
    for (DeviceAdminListItem item : mAdmins) {
        final String key = item.getKey();
        AppSwitchPreference pref = preferenceCache.remove(key);
        if (pref == null) {
            pref = new AppSwitchPreference(prefContext);
            mPreferenceGroup.addPreference(pref);
        }
        bindPreference(item, pref);
    }
    for (AppSwitchPreference unusedCacheItem : preferenceCache.values()) {
        mPreferenceGroup.removePreference(unusedCacheItem);
    }
}
Also used : Context(android.content.Context) AppSwitchPreference(com.android.settingslib.widget.AppSwitchPreference) AppSwitchPreference(com.android.settingslib.widget.AppSwitchPreference) Preference(androidx.preference.Preference) FooterPreference(com.android.settingslib.widget.FooterPreference) ArrayMap(android.util.ArrayMap)

Aggregations

AppSwitchPreference (com.android.settingslib.widget.AppSwitchPreference)2 ComponentName (android.content.ComponentName)1 Context (android.content.Context)1 PackageItemInfo (android.content.pm.PackageItemInfo)1 PackageManager (android.content.pm.PackageManager)1 ServiceInfo (android.content.pm.ServiceInfo)1 UserManager (android.os.UserManager)1 ArrayMap (android.util.ArrayMap)1 Preference (androidx.preference.Preference)1 PreferenceScreen (androidx.preference.PreferenceScreen)1 SwitchPreference (androidx.preference.SwitchPreference)1 FooterPreference (com.android.settingslib.widget.FooterPreference)1