Search in sources :

Example 26 with CheckBoxPreference

use of androidx.preference.CheckBoxPreference in project android_packages_apps_Settings by omnirom.

the class BridgedAppsPreferenceController method onRebuildComplete.

@Override
public void onRebuildComplete(ArrayList<AppEntry> apps) {
    if (apps == null) {
        return;
    }
    mNlf = mNm.getListenerFilter(mCn, mUserId);
    // Create apps key set for removing useless preferences
    final Set<String> appsKeySet = new TreeSet<>();
    // Add or update preferences
    final int N = apps.size();
    for (int i = 0; i < N; i++) {
        final AppEntry entry = apps.get(i);
        final String prefKey = entry.info.packageName + "|" + entry.info.uid;
        appsKeySet.add(prefKey);
        CheckBoxPreference preference = mScreen.findPreference(prefKey);
        if (preference == null) {
            preference = new CheckBoxPreference(mScreen.getContext());
            preference.setIcon(entry.icon);
            preference.setTitle(entry.label);
            preference.setKey(prefKey);
            mScreen.addPreference(preference);
        }
        preference.setOrder(i);
        preference.setChecked(mNlf.isPackageAllowed(new VersionedPackage(entry.info.packageName, entry.info.uid)));
        preference.setOnPreferenceChangeListener(this::onPreferenceChange);
    }
    // Remove preferences that are no longer existing in the updated list of apps
    removeUselessPrefs(appsKeySet);
}
Also used : AppEntry(com.android.settingslib.applications.ApplicationsState.AppEntry) CheckBoxPreference(androidx.preference.CheckBoxPreference) TreeSet(java.util.TreeSet) VersionedPackage(android.content.pm.VersionedPackage)

Example 27 with CheckBoxPreference

use of androidx.preference.CheckBoxPreference in project android_packages_apps_Settings by omnirom.

the class BridgedAppsPreferenceController method onPreferenceChange.

public boolean onPreferenceChange(Preference preference, Object newValue) {
    if (preference instanceof CheckBoxPreference) {
        String packageName = preference.getKey().substring(0, preference.getKey().indexOf("|"));
        int uid = Integer.parseInt(preference.getKey().substring(preference.getKey().indexOf("|") + 1));
        boolean allowlisted = newValue == Boolean.TRUE;
        mNlf = mNm.getListenerFilter(mCn, mUserId);
        if (allowlisted) {
            mNlf.removePackage(new VersionedPackage(packageName, uid));
        } else {
            mNlf.addPackage(new VersionedPackage(packageName, uid));
        }
        mNm.setListenerFilter(mCn, mUserId, mNlf);
        return true;
    }
    return false;
}
Also used : CheckBoxPreference(androidx.preference.CheckBoxPreference) VersionedPackage(android.content.pm.VersionedPackage)

Example 28 with CheckBoxPreference

use of androidx.preference.CheckBoxPreference in project android_packages_apps_Settings by omnirom.

the class RestrictedAppDetails method refreshUi.

@VisibleForTesting
void refreshUi() {
    mRestrictedAppListGroup.removeAll();
    final Context context = getPrefContext();
    final SparseLongArray timestampArray = mBatteryDatabaseManager.queryActionTime(AnomalyDatabaseHelper.ActionType.RESTRICTION);
    final long now = System.currentTimeMillis();
    for (int i = 0, size = mAppInfos.size(); i < size; i++) {
        final CheckBoxPreference checkBoxPreference = new AppCheckBoxPreference(context);
        final AppInfo appInfo = mAppInfos.get(i);
        try {
            final ApplicationInfo applicationInfo = mPackageManager.getApplicationInfoAsUser(appInfo.packageName, 0, /* flags */
            UserHandle.getUserId(appInfo.uid));
            checkBoxPreference.setChecked(mBatteryUtils.isForceAppStandbyEnabled(appInfo.uid, appInfo.packageName));
            checkBoxPreference.setTitle(mPackageManager.getApplicationLabel(applicationInfo));
            checkBoxPreference.setIcon(Utils.getBadgedIcon(mIconDrawableFactory, mPackageManager, appInfo.packageName, UserHandle.getUserId(appInfo.uid)));
            checkBoxPreference.setKey(getKeyFromAppInfo(appInfo));
            checkBoxPreference.setOnPreferenceChangeListener((pref, value) -> {
                final BatteryTipDialogFragment fragment = createDialogFragment(appInfo, (Boolean) value);
                fragment.setTargetFragment(this, 0);
                fragment.show(getFragmentManager(), TAG);
                mMetricsFeatureProvider.action(getContext(), SettingsEnums.ACTION_APP_RESTRICTED_LIST_UNCHECKED, appInfo.packageName);
                return false;
            });
            final long timestamp = timestampArray.get(appInfo.uid, TIME_NULL);
            if (timestamp != TIME_NULL) {
                checkBoxPreference.setSummary(getString(R.string.restricted_app_time_summary, StringUtil.formatRelativeTime(context, now - timestamp, false)));
            }
            final CharSequence test = checkBoxPreference.getSummaryOn();
            mRestrictedAppListGroup.addPreference(checkBoxPreference);
        } catch (PackageManager.NameNotFoundException e) {
            Log.e(TAG, "Can't find package: " + appInfo.packageName);
        }
    }
}
Also used : Context(android.content.Context) BatteryTipDialogFragment(com.android.settings.fuelgauge.batterytip.BatteryTipDialogFragment) PackageManager(android.content.pm.PackageManager) CheckBoxPreference(androidx.preference.CheckBoxPreference) AppCheckBoxPreference(com.android.settings.widget.AppCheckBoxPreference) AppCheckBoxPreference(com.android.settings.widget.AppCheckBoxPreference) ApplicationInfo(android.content.pm.ApplicationInfo) SparseLongArray(android.util.SparseLongArray) AppInfo(com.android.settings.fuelgauge.batterytip.AppInfo) VisibleForTesting(androidx.annotation.VisibleForTesting)

Example 29 with CheckBoxPreference

use of androidx.preference.CheckBoxPreference in project android_packages_apps_Settings by omnirom.

the class BridgedAppsPreferenceControllerTest method onPreferenceChange_false.

@Test
public void onPreferenceChange_false() {
    VersionedPackage vp = new VersionedPackage("pkg", 10567);
    ArraySet<VersionedPackage> vps = new ArraySet<>();
    vps.add(vp);
    NotificationListenerFilter nlf = new NotificationListenerFilter(FLAG_FILTER_TYPE_ONGOING | FLAG_FILTER_TYPE_CONVERSATIONS, vps);
    when(mNm.isNotificationListenerAccessGranted(mCn)).thenReturn(true);
    when(mNm.getListenerFilter(mCn, 0)).thenReturn(nlf);
    CheckBoxPreference pref = new CheckBoxPreference(mContext);
    pref.setKey("pkg|567");
    mController.onPreferenceChange(pref, false);
    ArgumentCaptor<NotificationListenerFilter> captor = ArgumentCaptor.forClass(NotificationListenerFilter.class);
    verify(mNm).setListenerFilter(eq(mCn), eq(0), captor.capture());
    assertThat(captor.getValue().getDisallowedPackages()).contains(new VersionedPackage("pkg", 567));
    assertThat(captor.getValue().getDisallowedPackages()).contains(new VersionedPackage("pkg", 10567));
}
Also used : ArraySet(android.util.ArraySet) CheckBoxPreference(androidx.preference.CheckBoxPreference) VersionedPackage(android.content.pm.VersionedPackage) NotificationListenerFilter(android.service.notification.NotificationListenerFilter) Test(org.junit.Test)

Example 30 with CheckBoxPreference

use of androidx.preference.CheckBoxPreference in project android_packages_apps_Settings by omnirom.

the class BridgedAppsPreferenceControllerTest method onRebuildComplete_buildsSetting.

@Test
public void onRebuildComplete_buildsSetting() {
    when(mNm.isNotificationListenerAccessGranted(mCn)).thenReturn(true);
    when(mNm.getListenerFilter(mCn, 0)).thenReturn(new NotificationListenerFilter());
    ArrayList<ApplicationsState.AppEntry> entries = new ArrayList<>();
    entries.add(mAppEntry);
    mController.onRebuildComplete(entries);
    CheckBoxPreference actual = mScreen.findPreference("pkg|12300");
    assertThat(actual.isChecked()).isTrue();
    assertThat(actual.getTitle()).isEqualTo("hi");
    assertThat(actual.getIcon()).isEqualTo(mAppEntry.icon);
}
Also used : CheckBoxPreference(androidx.preference.CheckBoxPreference) ArrayList(java.util.ArrayList) NotificationListenerFilter(android.service.notification.NotificationListenerFilter) Test(org.junit.Test)

Aggregations

CheckBoxPreference (androidx.preference.CheckBoxPreference)36 Test (org.junit.Test)16 NotificationListenerFilter (android.service.notification.NotificationListenerFilter)14 Preference (androidx.preference.Preference)8 ListPreference (androidx.preference.ListPreference)7 Bundle (android.os.Bundle)6 VersionedPackage (android.content.pm.VersionedPackage)4 SparseLongArray (android.util.SparseLongArray)4 AppInfo (com.android.settings.fuelgauge.batterytip.AppInfo)4 AppCheckBoxPreference (com.android.settings.widget.AppCheckBoxPreference)4 Context (android.content.Context)3 SharedPreferences (android.content.SharedPreferences)3 PackageManager (android.content.pm.PackageManager)3 ArraySet (android.util.ArraySet)3 EditTextPreference (androidx.preference.EditTextPreference)3 PreferenceCategory (androidx.preference.PreferenceCategory)3 SeekBarPreference (androidx.preference.SeekBarPreference)3 ArrayList (java.util.ArrayList)3 ApplicationInfo (android.content.pm.ApplicationInfo)2 VisibleForTesting (androidx.annotation.VisibleForTesting)2