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

Commit 1a2ad98c authored by Roman Birg's avatar Roman Birg Committed by Gerrit Code Review
Browse files

SystemUI: protect Protected App notifications



Change-Id: I85ff0e8c0ba0fcfc0e1426f2d2b102784f437847
Signed-off-by: default avatarRoman Birg <roman@cyngn.com>
parent 85dbec9b
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -112,4 +112,7 @@
    <string name="sim_slot_3" translatable="false">3</string>

    <string name="status_bar_notification_spam_item_title">Ignore messages like this</string>

    <string name="protected_app_notification_title">Protected app notification</string>
    <string name="protected_app_notification_summary">Tap to open notification from protected app</string>
</resources>
+118 −28
Original line number Diff line number Diff line
@@ -490,11 +490,16 @@ public abstract class BaseStatusBar extends SystemUI implements
                final String packageNameF = sbNotification.getPackageName();
                if (packageNameF == null) return false;
                if (v.getWindowToken() == null) return false;

                // protected apps don't get long click listener
                if (isPackageProtected(packageNameF)) return false;

                mNotificationBlamePopup = new PopupMenu(mContext, v);
                mNotificationBlamePopup.getMenuInflater().inflate(
                        R.menu.notification_popup_menu,
                        mNotificationBlamePopup.getMenu());
                final ContentResolver cr = mContext.getContentResolver();

                if (Settings.Secure.getInt(cr,
                        Settings.Secure.DEVELOPMENT_SHORTCUT, 0) == 0) {
                    mNotificationBlamePopup.getMenu()
@@ -506,9 +511,10 @@ public abstract class BaseStatusBar extends SystemUI implements
                } else {
                    try {
                        PackageManager pm = (PackageManager) mContext.getPackageManager();
                        ApplicationInfo mAppInfo = pm.getApplicationInfo(packageNameF, 0);
                        ApplicationInfo mAppInfo = null;
                        DevicePolicyManager mDpm = (DevicePolicyManager) mContext.
                                getSystemService(Context.DEVICE_POLICY_SERVICE);
                        mAppInfo = pm.getApplicationInfo(packageNameF, 0);
                        if ((mAppInfo.flags&(ApplicationInfo.FLAG_SYSTEM
                                | ApplicationInfo.FLAG_ALLOW_CLEAR_USER_DATA))
                                == ApplicationInfo.FLAG_SYSTEM
@@ -818,6 +824,20 @@ public abstract class BaseStatusBar extends SystemUI implements

        View contentViewLocal = null;
        View bigContentViewLocal = null;

        if (isPackageProtected(entry.notification.getPackageName())) {
            // let's construct a new notification for this protected app
            ViewGroup base = (ViewGroup) inflater.inflate(com.android.internal.R.layout.notification_template_base, row, false);
            TextView title = (TextView) base.findViewById(com.android.internal.R.id.title);
            title.setText(R.string.protected_app_notification_title);
            TextView text2 = (TextView) base.findViewById(com.android.internal.R.id.text2);
            text2.setVisibility(View.VISIBLE);
            text2.setText(R.string.protected_app_notification_summary);
            ImageView icon = (ImageView) base.findViewById(com.android.internal.R.id.icon);
            icon.setImageResource(R.drawable.ic_protected_apps);

            contentViewLocal = base;
        } else {
            final ThemeConfig themeConfig = mContext.getResources().getConfiguration().themeConfig;
            String themePackageName = themeConfig != null ?
                    themeConfig.getOverlayPkgNameForApp(mContext.getPackageName()) : null;
@@ -828,12 +848,12 @@ public abstract class BaseStatusBar extends SystemUI implements
                    bigContentViewLocal = bigContentView.apply(mContext, adaptive, mOnClickHandler,
                            themePackageName);
                }
        }
        catch (RuntimeException e) {
            } catch (RuntimeException e) {
                final String ident = sbn.getPackageName() + "/0x" + Integer.toHexString(sbn.getId());
                Log.e(TAG, "couldn't inflate view for notification " + ident, e);
                return false;
            }
        }

        if (contentViewLocal != null) {
            SizeAdaptiveLayout.LayoutParams params =
@@ -874,16 +894,31 @@ public abstract class BaseStatusBar extends SystemUI implements
    }

    protected class NotificationClicker implements View.OnClickListener {
        public static final String ACTION_BROADCAST_RESULT =
                "com.android.settings.applications.LockPatternActivity.ACTION_BROADCAST_RESULT";

        // used for incoming and outgoing broadcast intents
        public static final String EXTRA_BROADCAST_RESULT = "broadcast_result";

        public final ComponentName LOCK_PATTERN_COMPONENT =
                new ComponentName("com.android.settings",
                        "com.android.settings.applications.LockPatternActivity");


        private PendingIntent mIntent;
        private String mPkg;
        private String mTag;
        private int mId;
        private boolean mProtected;

        private BroadcastReceiver mProtectedAppReceiver;

        public NotificationClicker(PendingIntent intent, String pkg, String tag, int id) {
            mIntent = intent;
            mPkg = pkg;
            mTag = tag;
            mId = id;
            mProtected = isPackageProtected(pkg);
        }

        public void onClick(View v) {
@@ -899,12 +934,43 @@ public abstract class BaseStatusBar extends SystemUI implements
            } catch (RemoteException e) {
            }

            if (mIntent != null) {
            int[] pos = new int[2];
            v.getLocationOnScreen(pos);
            final Rect r = new Rect(pos[0], pos[1], pos[0]+v.getWidth(), pos[1]+v.getHeight());

            if (mProtected) {
                mProtectedAppReceiver = new BroadcastReceiver() {
                    @Override
                    public void onReceive(Context context, Intent intent) {
                        boolean result = intent.getBooleanExtra("broadcast_result", false);
                        if (result) {
                            doRealClick(r);
                        }
                        mContext.unregisterReceiver(this);
                    }
                };
                mContext.registerReceiver(mProtectedAppReceiver,
                        new IntentFilter(ACTION_BROADCAST_RESULT));
                Intent protectedIntent = new Intent();
                protectedIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                protectedIntent.setComponent(LOCK_PATTERN_COMPONENT);
                protectedIntent.putExtra(EXTRA_BROADCAST_RESULT, true);
                mContext.startActivity(protectedIntent);

                // close the shade if it was open
                animateCollapsePanels(CommandQueue.FLAG_EXCLUDE_NONE);
                visibilityChanged(false);
                return;
            } else {
                doRealClick(r);
            }
        }

        private void doRealClick(Rect r) {
            if (mIntent != null) {

                Intent overlay = new Intent();
                overlay.setSourceBounds(
                        new Rect(pos[0], pos[1], pos[0]+v.getWidth(), pos[1]+v.getHeight()));
                overlay.setSourceBounds(r);
                try {
                    mIntent.send(mContext, 0, overlay);
                } catch (PendingIntent.CanceledException e) {
@@ -999,18 +1065,42 @@ public abstract class BaseStatusBar extends SystemUI implements
        }
    }

    private boolean isPackageProtected(String packageName) {
        try {
            ApplicationInfo info = mContext.getPackageManager().getApplicationInfo(packageName, 0);
            return info.protect;
        } catch (NameNotFoundException e) {
            Log.d(TAG, "protected package not found.", e);
        }
        return false;
    }

    protected NotificationData.Entry createNotificationViews(IBinder key,
            StatusBarNotification notification) {
        if (DEBUG) {
            Log.d(TAG, "createNotificationViews(key=" + key + ", notification=" + notification);
        }

        boolean protectedApp = isPackageProtected(notification.getPackageName());
        if (protectedApp) {
            notification.getNotification().icon = com.android.systemui.R.drawable.ic_protected_apps;
            notification.getNotification().iconLevel = 0;
            notification.getNotification().number = 0;
            if (notification.getNotification().tickerText != null) {
                // null out ticker
                notification.getNotification().tickerText = null;
            }
        }

        // Construct the icon.
        final StatusBarIconView iconView = new StatusBarIconView(mContext,
                notification.getPackageName() + "/0x" + Integer.toHexString(notification.getId()),
                notification.getNotification());
        iconView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

        final StatusBarIcon ic = new StatusBarIcon(notification.getPackageName(),
        final StatusBarIcon ic = new StatusBarIcon(
                // for protected apps we need to use system ui's package name
                protectedApp ? mContext.getPackageName() : notification.getPackageName(),
                notification.getUser(),
                notification.getNotification().icon,
                notification.getNotification().iconLevel,