Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 679fde4c authored by Julia Reynolds's avatar Julia Reynolds Committed by Android (Google) Code Review
Browse files

Merge "Show app level settings when app doesn't use topics."

parents 3ed17521 92ea89c3
Loading
Loading
Loading
Loading
+5 −3
Original line number Diff line number Diff line
@@ -58,7 +58,8 @@
                    android:src="@*android:drawable/ic_notification_block"
                    android:layout_gravity="center_vertical|start"
                    android:layout_width="24dp"
                    android:layout_height="24dp" />
                    android:layout_height="24dp"
                    android:tint="@color/importance_icon_tint" />

            <SeekBar
                    android:id="@*android:id/seekbar"
@@ -66,7 +67,7 @@
                    android:layout_marginEnd="56dp"
                    android:layout_gravity="center_vertical"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_height="48dp"
                    android:focusable="true"
                    android:background="#00ffffff"
                    style="@android:style/Widget.Material.SeekBar.Discrete"
@@ -77,7 +78,8 @@
                    android:src="@*android:drawable/ic_notification_alert"
                    android:layout_gravity="center_vertical|end"
                    android:layout_width="24dp"
                    android:layout_height="24dp" />
                    android:layout_height="24dp"
                    android:tint="@color/importance_icon_tint" />

        </FrameLayout>

+3 −0
Original line number Diff line number Diff line
@@ -104,6 +104,9 @@
    <color name="summary_default_start">#ff009587</color>
    <color name="summary_default_end">#ffced7db</color>

    <color name="importance_icon_tint">#8a000000</color>
    <color name="importance_disabled_tint">#4d000000</color>

    <!-- Accessibility SUW colors -->
    <color name="material_blue_500">#4285F4</color>
    <color name="material_blue_700">#3367D6</color>
+1 −15
Original line number Diff line number Diff line
@@ -18,24 +18,10 @@
        android:title="@string/app_notifications_title"
        android:key="app_notification_settings">

    <!-- Block -->
    <SwitchPreference
            android:key="block"
            android:title="@string/app_notification_block_title"
            android:summary="@string/app_notification_block_summary"
            android:order="1"
            android:persistent="false" />

     <!-- App notification preferences -->
    <Preference
            android:key="app_settings"
            android:title="@string/app_notification_preferences"
            android:order="2"
            android:persistent="false" />

    <PreferenceCategory
            android:key="categories"
            android:title="@string/notification_topic_categories"
            android:order="3" />

</PreferenceScreen>
+66 −136
Original line number Diff line number Diff line
@@ -17,33 +17,23 @@
package com.android.settings.notification;

import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.os.UserHandle;
import android.provider.Settings;
import android.service.notification.NotificationListenerService;
import android.support.v14.preference.SwitchPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.Preference.OnPreferenceChangeListener;
import android.support.v7.preference.Preference.OnPreferenceClickListener;
import android.support.v7.preference.PreferenceCategory;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Log;
import android.widget.Toast;

import com.android.internal.logging.MetricsLogger;
import com.android.internal.widget.LockPatternUtils;
import com.android.settings.AppHeader;
import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.Utils;
import com.android.settings.applications.AppInfoBase;
import com.android.settings.notification.NotificationBackend.AppRow;
@@ -51,7 +41,7 @@ import com.android.settings.notification.NotificationBackend.AppRow;
import java.util.List;

/** These settings are per app, so should not be returned in global search results. */
public class AppNotificationSettings extends SettingsPreferenceFragment {
public class AppNotificationSettings extends NotificationSettingsBase {
    private static final String TAG = "AppNotificationSettings";
    private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);

@@ -63,24 +53,13 @@ public class AppNotificationSettings extends SettingsPreferenceFragment {
            = new Intent(Intent.ACTION_MAIN)
                .addCategory(Notification.INTENT_CATEGORY_NOTIFICATION_PREFERENCES);

    private final NotificationBackend mBackend = new NotificationBackend();

    private Context mContext;
    private SwitchPreference mBlock;
    private PreferenceCategory mCategories;
    private AppRow mAppRow;
    private boolean mCreated;
    private int mUid;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (DEBUG) Log.d(TAG, "onActivityCreated mCreated=" + mCreated);
        if (mCreated) {
            Log.w(TAG, "onActivityCreated: ignoring duplicate call");
            return;
        }
        mCreated = true;
        if (mAppRow == null) return;
        AppHeader.createAppHeader(this, mAppRow.icon, mAppRow.label, mAppRow.pkg, mAppRow.uid);
    }
@@ -93,53 +72,31 @@ public class AppNotificationSettings extends SettingsPreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
        Intent intent = getActivity().getIntent();
        Bundle args = getArguments();
        if (DEBUG) Log.d(TAG, "onCreate getIntent()=" + intent);
        if (intent == null && args == null) {
            Log.w(TAG, "No intent");
            toastAndFinish();
            return;
        }

        final String pkg = args != null && args.containsKey(AppInfoBase.ARG_PACKAGE_NAME)
                ? args.getString(AppInfoBase.ARG_PACKAGE_NAME)
                : intent.getStringExtra(Settings.EXTRA_APP_PACKAGE);
        mUid = args != null && args.containsKey(AppInfoBase.ARG_PACKAGE_UID)
                ? args.getInt(AppInfoBase.ARG_PACKAGE_UID)
                : intent.getIntExtra(Settings.EXTRA_APP_UID, -1);
        if (mUid == -1 || TextUtils.isEmpty(pkg)) {
            Log.w(TAG, "Missing extras: " + Settings.EXTRA_APP_PACKAGE + " was " + pkg + ", "
                    + Settings.EXTRA_APP_UID + " was " + mUid);
            toastAndFinish();
            return;
        }

        if (DEBUG) Log.d(TAG, "Load details for pkg=" + pkg + " uid=" + mUid);
        final PackageManager pm = getPackageManager();
        final PackageInfo info = findPackageInfo(pm, pkg, mUid);
        if (info == null) {
            Log.w(TAG, "Failed to find package info: " + Settings.EXTRA_APP_PACKAGE + " was " + pkg
                    + ", " + Settings.EXTRA_APP_UID + " was " + mUid);
            toastAndFinish();
            return;
        }
        addPreferencesFromResource(R.xml.app_notification_settings);
        mBlock = (SwitchPreference) findPreference(KEY_BLOCK);

        mAppRow = mBackend.loadAppRow(pm, info);
        getPreferenceScreen().setOrderingAsAdded(true);
        mAppRow = mBackend.loadAppRow(mPm, mPkgInfo);

        // load settings intent
        ArrayMap<String, AppRow> rows = new ArrayMap<String, AppRow>();
        rows.put(mAppRow.pkg, mAppRow);
        collectConfigActivities(getPackageManager(), rows);
        collectConfigActivities(rows);

        // Add topics
        List<Notification.Topic> topics = mBackend.getTopics(pkg, mUid);
        mCategories = (PreferenceCategory) getPreferenceScreen().findPreference(KEY_CATEGORIES);
        List<Notification.Topic> topics = mBackend.getTopics(mPkg, mUid);
        if (topics.size() <= 1) {
            setupImportancePref(mAppRow, null, mAppRow.appImportance);
            setupPriorityPref(null, mAppRow.appBypassDnd);
            setupSensitivePref(null, mAppRow.appSensitive);
        } else {
            setupBlockSwitch();
            mCategories = new PreferenceCategory(getPrefContext());
            mCategories.setKey(KEY_CATEGORIES);
            mCategories.setTitle(R.string.notification_topic_categories);
            mCategories.setOrderingAsAdded(true);
            getPreferenceScreen().addPreference(mCategories);
            for (Notification.Topic topic : topics) {
            Preference topicPreference = new Preference(mContext);
                Preference topicPreference = new Preference(getPrefContext());
                topicPreference.setKey(topic.getId());
                topicPreference.setTitle(topic.getLabel());
                // Create intent for this preference.
@@ -147,7 +104,8 @@ public class AppNotificationSettings extends SettingsPreferenceFragment {
                topicArgs.putInt(AppInfoBase.ARG_PACKAGE_UID, mUid);
                topicArgs.putParcelable(TopicNotificationSettings.ARG_TOPIC, topic);
                topicArgs.putBoolean(AppHeader.EXTRA_HIDE_INFO_BUTTON, true);
            topicArgs.putParcelable(TopicNotificationSettings.ARG_PACKAGE_INFO, info);
                topicArgs.putString(AppInfoBase.ARG_PACKAGE_NAME, mPkg);
                topicArgs.putParcelable(TopicNotificationSettings.ARG_PACKAGE_INFO, mPkgInfo);

                Intent topicIntent = Utils.onBuildStartFragmentIntent(getActivity(),
                        TopicNotificationSettings.class.getName(),
@@ -155,24 +113,7 @@ public class AppNotificationSettings extends SettingsPreferenceFragment {
                topicPreference.setIntent(topicIntent);
                mCategories.addPreference(topicPreference);
            }

        mBlock.setChecked(mAppRow.banned);
        updateDependents(mAppRow.systemApp, mAppRow.banned);

        mBlock.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                final boolean banned = (Boolean) newValue;
                if (banned) {
                    MetricsLogger.action(getActivity(), MetricsLogger.ACTION_BAN_APP_NOTES, pkg);
                }
                final boolean success =  mBackend.setNotificationsBanned(pkg, mUid, banned);
                if (success) {
                    updateDependents(mAppRow.systemApp, banned);
                }
                return success;
        }
        });

        if (mAppRow.settingsIntent != null) {
            findPreference(KEY_APP_SETTINGS).setOnPreferenceClickListener(
@@ -189,70 +130,59 @@ public class AppNotificationSettings extends SettingsPreferenceFragment {
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mUid != -1 && getPackageManager().getPackagesForUid(mUid) == null) {
            // App isn't around anymore, must have been removed.
            finish();
        }
    protected void updateDependents(int progress) {
        updateDependents(progress == NotificationListenerService.Ranking.IMPORTANCE_NONE);
    }

    private void updateDependents(boolean isSystemPackage, boolean banned) {
        setVisible(mBlock, !isSystemPackage);
    private void updateDependents(boolean banned) {
        if (mBlock != null) {
            mBlock.setEnabled(!mAppRow.systemApp);
        }
        if (mCategories != null) {
            setVisible(mCategories, !banned);
        }
    }

    private void setVisible(Preference p, boolean visible) {
        final boolean isVisible = getPreferenceScreen().findPreference(p.getKey()) != null;
        if (isVisible == visible) return;
        if (visible) {
            getPreferenceScreen().addPreference(p);
        } else {
            getPreferenceScreen().removePreference(p);
        }
    }

    private void toastAndFinish() {
        Toast.makeText(mContext, R.string.app_not_found_dlg_text, Toast.LENGTH_SHORT).show();
        getActivity().finish();
    }

    private static PackageInfo findPackageInfo(PackageManager pm, String pkg, int uid) {
        final String[] packages = pm.getPackagesForUid(uid);
        if (packages != null && pkg != null) {
            final int N = packages.length;
            for (int i = 0; i < N; i++) {
                final String p = packages[i];
                if (pkg.equals(p)) {
                    try {
                        return pm.getPackageInfo(pkg, PackageManager.GET_SIGNATURES);
                    } catch (NameNotFoundException e) {
                        Log.w(TAG, "Failed to load package " + pkg, e);
                    }
    private void setupBlockSwitch() {
        mBlock = new SwitchPreference(getPrefContext());
        mBlock.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                final boolean banned = (Boolean) newValue;
                if (banned) {
                    MetricsLogger.action(getActivity(), MetricsLogger.ACTION_BAN_APP_NOTES, mPkg);
                }
                final boolean success =  mBackend.setNotificationsBanned(mPkg, mUid, banned);
                if (success) {
                    updateDependents(banned);
                }
                return success;
            }
        return null;
        });
        mBlock.setKey(KEY_BLOCK);
        mBlock.setTitle(R.string.app_notification_block_title);
        mBlock.setSummary(R.string.app_notification_block_summary);
        getPreferenceScreen().addPreference(mBlock);
        mBlock.setChecked(mAppRow.banned);
        updateDependents(mAppRow.banned);
    }

    public static List<ResolveInfo> queryNotificationConfigActivities(PackageManager pm) {
    private List<ResolveInfo> queryNotificationConfigActivities() {
        if (DEBUG) Log.d(TAG, "APP_NOTIFICATION_PREFS_CATEGORY_INTENT is "
                + APP_NOTIFICATION_PREFS_CATEGORY_INTENT);
        final List<ResolveInfo> resolveInfos = pm.queryIntentActivities(
        final List<ResolveInfo> resolveInfos = mPm.queryIntentActivities(
                APP_NOTIFICATION_PREFS_CATEGORY_INTENT,
                0 //PackageManager.MATCH_DEFAULT_ONLY
        );
        return resolveInfos;
    }

    public static void collectConfigActivities(PackageManager pm, ArrayMap<String, AppRow> rows) {
        final List<ResolveInfo> resolveInfos = queryNotificationConfigActivities(pm);
        applyConfigActivities(pm, rows, resolveInfos);
    private void collectConfigActivities(ArrayMap<String, AppRow> rows) {
        final List<ResolveInfo> resolveInfos = queryNotificationConfigActivities();
        applyConfigActivities(rows, resolveInfos);
    }

    public static void applyConfigActivities(PackageManager pm, ArrayMap<String, AppRow> rows,
    private void applyConfigActivities(ArrayMap<String, AppRow> rows,
            List<ResolveInfo> resolveInfos) {
        if (DEBUG) Log.d(TAG, "Found " + resolveInfos.size() + " preference activities"
                + (resolveInfos.size() == 0 ? " ;_;" : ""));
+13 −1
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import android.content.Context;
import android.service.notification.NotificationListenerService;
import android.support.v7.preference.PreferenceViewHolder;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

@@ -37,6 +38,7 @@ public class ImportanceSeekBarPreference extends SeekBarPreference implements
    private TextView mSummaryTextView;
    private String mSummary;
    private int mMinProgress;
    private boolean mSystemApp;

    public ImportanceSeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr,
            int defStyleRes) {
@@ -64,10 +66,19 @@ public class ImportanceSeekBarPreference extends SeekBarPreference implements
        mMinProgress = minProgress;
    }

    public void setSystemApp(boolean systemApp) {
        mSystemApp = systemApp;
        notifyChanged();
    }

    @Override
    public void onBindViewHolder(PreferenceViewHolder view) {
        super.onBindViewHolder(view);
        mSummaryTextView = (TextView) view.findViewById(com.android.internal.R.id.summary);
        if (mSystemApp) {
            ((ImageView) view.findViewById(R.id.low_importance)).getDrawable().setTint(
                    getContext().getColor(R.color.importance_disabled_tint));
        }
    }

    @Override
@@ -83,8 +94,9 @@ public class ImportanceSeekBarPreference extends SeekBarPreference implements
            seekBar.setProgress(mMinProgress);
            progress = mMinProgress;
        }
        mSummary = getProgressSummary(progress);
        if (mSummaryTextView != null) {
            mSummaryTextView.setText(getProgressSummary(progress));
            mSummaryTextView.setText(mSummary);
        }
        if (fromTouch) {
            mCallback.onImportanceChanged(progress);
Loading