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

Commit 3f622216 authored by Sam Mortimer's avatar Sam Mortimer
Browse files

AppOps: menu options to show/hide user and system apps

Change-Id: Ia55364c361db949a891e4b53a757ee4dff519eb6
parent 60e88de4
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2013 The CyanogenMod Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/show_user_apps"
          android:title="@string/app_ops_show_user_apps"
          android:checkable="true" />
    <item android:id="@+id/show_system_apps"
          android:title="@string/app_ops_show_system_apps"
          android:checkable="true" />
</menu>
+4 −0
Original line number Diff line number Diff line
@@ -1026,4 +1026,8 @@ two in order to insert additional control points. \'Remove\' deletes the selecte
    <string name="app_ops_labels_bluetooth_change">Bluetooth change</string>
    <string name="app_ops_summaries_data_change">data change</string>
    <string name="app_ops_labels_data_change">Data change</string>
    <!-- App ops menu options -->
    <string name="app_ops_show_user_apps">Show user apps</string>
    <string name="app_ops_show_system_apps">Show built-in apps</string>

</resources>
+36 −10
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.settings.applications;

import android.app.Activity;
import android.app.AppOpsManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
@@ -23,6 +24,7 @@ import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Parcel;
import android.os.Parcelable;
@@ -52,12 +54,15 @@ public class AppOpsState {

    List<AppOpEntry> mApps;

    private SharedPreferences mPreferences;

    public AppOpsState(Context context) {
        mContext = context;
        mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);
        mPm = context.getPackageManager();
        mOpSummaries = context.getResources().getTextArray(R.array.app_ops_summaries_cm);
        mOpLabels = context.getResources().getTextArray(R.array.app_ops_labels_cm);
        mPreferences = context.getSharedPreferences("appops_manager", Activity.MODE_PRIVATE);
    }

    public static class OpsTemplate implements Parcelable {
@@ -441,8 +446,7 @@ public class AppOpsState {

    private AppEntry getAppEntry(final Context context, final HashMap<String, AppEntry> appEntries,
            final String packageName, ApplicationInfo appInfo) {
        AppEntry appEntry = appEntries.get(packageName);
        if (appEntry == null) {

        if (appInfo == null) {
            try {
                appInfo = mPm.getApplicationInfo(packageName,
@@ -453,6 +457,20 @@ public class AppOpsState {
                return null;
            }
        }

        // Hide user apps if needed
        if (!shouldShowUserApps() &&
                (appInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
            return null;
        }
        // Hide system apps if needed
        if (!shouldShowSystemApps() &&
                (appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
            return null;
        }

        AppEntry appEntry = appEntries.get(packageName);
        if (appEntry == null) {
            appEntry = new AppEntry(this, appInfo);
            appEntry.loadLabel(context);
            appEntries.put(packageName, appEntry);
@@ -460,6 +478,14 @@ public class AppOpsState {
        return appEntry;
    }

    private boolean shouldShowUserApps() {
        return mPreferences.getBoolean("show_user_apps", true);
    }

    private boolean shouldShowSystemApps() {
        return mPreferences.getBoolean("show_system_apps", true);
    }

    public List<AppOpEntry> buildState(OpsTemplate tpl, int uid, String packageName) {
        final Context context = mContext;

+77 −3
Original line number Diff line number Diff line
@@ -16,15 +16,20 @@

package com.android.settings.applications;

import android.app.Activity;
import android.app.AppOpsManager;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceFrameLayout;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

@@ -38,6 +43,11 @@ public class AppOpsSummary extends Fragment {
    private View mRootView;
    private ViewPager mViewPager;

    private MyPagerAdapter mAdapter;

    private Activity mActivity;
    private SharedPreferences mPreferences;

    CharSequence[] mPageNames;
    static AppOpsState.OpsTemplate[] sPageTemplates = new AppOpsState.OpsTemplate[] {
        AppOpsState.LOCATION_TEMPLATE,
@@ -78,6 +88,10 @@ public class AppOpsSummary extends Fragment {
            mCurPos = position;
        }

        public int getCurrentPage() {
            return mCurPos;
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            if (state == ViewPager.SCROLL_STATE_IDLE) {
@@ -86,6 +100,14 @@ public class AppOpsSummary extends Fragment {
        }
    }

    private void resetAdapter() {
        // trigger adapter load, preserving the selected page
        int curPos = mAdapter.getCurrentPage();
        mViewPager.setAdapter(mAdapter);
        mViewPager.setOnPageChangeListener(mAdapter);
        mViewPager.setCurrentItem(curPos);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // initialize the inflater
@@ -99,9 +121,9 @@ public class AppOpsSummary extends Fragment {
        mPageNames = getResources().getTextArray(R.array.app_ops_categories);

        mViewPager = (ViewPager) rootView.findViewById(R.id.pager);
        MyPagerAdapter adapter = new MyPagerAdapter(getChildFragmentManager());
        mViewPager.setAdapter(adapter);
        mViewPager.setOnPageChangeListener(adapter);
        mAdapter = new MyPagerAdapter(getChildFragmentManager());
        mViewPager.setAdapter(mAdapter);
        mViewPager.setOnPageChangeListener(mAdapter);
        PagerTabStrip tabs = (PagerTabStrip) rootView.findViewById(R.id.tabs);
        tabs.setTabIndicatorColorResource(android.R.color.holo_blue_light);

@@ -111,6 +133,58 @@ public class AppOpsSummary extends Fragment {
            ((PreferenceFrameLayout.LayoutParams) rootView.getLayoutParams()).removeBorders = true;
        }

        mActivity = getActivity();

        return rootView;
    }

    private boolean shouldShowUserApps() {
        return mPreferences.getBoolean("show_user_apps", true);
    }

    private boolean shouldShowSystemApps() {
        return mPreferences.getBoolean("show_system_apps", true);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        // get shared preferences
        mPreferences = mActivity.getSharedPreferences("appops_manager", Activity.MODE_PRIVATE);

        setHasOptionsMenu(true);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.appops_manager, menu);
        menu.findItem(R.id.show_user_apps).setChecked(shouldShowUserApps());
        menu.findItem(R.id.show_system_apps).setChecked(shouldShowSystemApps());
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.show_user_apps:
                final String prefNameUserApps = "show_user_apps";
                // set the menu checkbox and save it in shared preference
                item.setChecked(!item.isChecked());
                mPreferences.edit().putBoolean(prefNameUserApps, item.isChecked()).commit();
                // reload content
                resetAdapter();
                return true;
            case R.id.show_system_apps:
                final String prefNameSysApps = "show_system_apps";
                // set the menu checkbox and save it in shared preference
                item.setChecked(!item.isChecked());
                mPreferences.edit().putBoolean(prefNameSysApps, item.isChecked()).commit();
                // reload view content
                resetAdapter();
                return true;
            default:
                return super.onContextItemSelected(item);
        }
    }
}