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

Commit 3a6c509d authored by Danny Baumann's avatar Danny Baumann Committed by Steve Kondik
Browse files

Fix AIOOBE with root access disabled.

Page title and template array lengths got out of sync if root access was
disabled. Make sure this doesn't happen anymore by combining titles and
templates into a single list prior to filtering.

Change-Id: I069db9b9925887d6d80592eabe93b33abeaf0fe8
JIRA: CYAN-8028
parent 48afc16f
Loading
Loading
Loading
Loading
+33 −30
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ import android.preference.PreferenceFrameLayout;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.util.Pair;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.Menu;
@@ -62,37 +63,35 @@ public class AppOpsSummary extends InstrumentedFragment {
    private Activity mActivity;
    private SharedPreferences mPreferences;

    CharSequence[] mPageNames;

    int mCurPos;
    int mPositionOffset;

    @Override
    protected int getMetricsCategory() {
        return MetricsEvent.APP_OPS_SUMMARY;
    }

    class MyPagerAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener {
        private AppOpsState.OpsTemplate[] mPageTemplates;
    static class MyPagerAdapter extends FragmentPagerAdapter
            implements ViewPager.OnPageChangeListener {
        private List<Pair<CharSequence, AppOpsState.OpsTemplate>> mPageData;
        private int mCurPos;

        public MyPagerAdapter(FragmentManager fm, AppOpsState.OpsTemplate[] templates) {
        public MyPagerAdapter(FragmentManager fm,
                List<Pair<CharSequence, AppOpsState.OpsTemplate>> data) {
            super(fm);
            mPageTemplates = templates;
            mPageData = data;
        }

        @Override
        public Fragment getItem(int position) {
            return new AppOpsCategory(mPageTemplates[mPositionOffset + position]);
            return new AppOpsCategory(mPageData.get(position).second);
        }

        @Override
        public int getCount() {
            return mPageNames.length;
            return mPageData.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mPageNames[position];
            return mPageData.get(position).first;
        }

        @Override
@@ -134,23 +133,24 @@ public class AppOpsSummary extends InstrumentedFragment {
        mContentContainer = container;
        mRootView = rootView;

        mPageNames = getResources().getTextArray(R.array.app_ops_categories_cm);

        mPositionOffset = 0;
        CharSequence[] pageNames = getResources().getTextArray(R.array.app_ops_categories_cm);
        AppOpsState.OpsTemplate[] templates = AppOpsState.ALL_TEMPLATES;
        assert(pageNames.length == templates.length);

        int specificTab = -1;
        Bundle bundle = getArguments();
        if (bundle != null) {
            specificTab = Arrays.asList(mPageNames).indexOf(bundle.getString("appops_tab", ""));
            if (specificTab >= 0) {
                mPageNames = Arrays.copyOfRange(mPageNames, specificTab, specificTab + 1);
                mPositionOffset = specificTab;
            specificTab = Arrays.asList(pageNames).indexOf(bundle.getString("appops_tab", ""));
        }

        List<Pair<CharSequence, AppOpsState.OpsTemplate>> pageData = new ArrayList<>();
        for (int i = 0; i < pageNames.length; i++) {
            pageData.add(Pair.create(pageNames[i], templates[i]));
        }
        filterPageData(pageData, specificTab);

        mViewPager = (ViewPager) rootView.findViewById(R.id.pager);
        mAdapter = new MyPagerAdapter(getChildFragmentManager(),
                filterTemplates(AppOpsState.ALL_TEMPLATES));
        mAdapter = new MyPagerAdapter(getChildFragmentManager(), pageData);
        mViewPager.setAdapter(mAdapter);
        mViewPager.setOnPageChangeListener(mAdapter);
        PagerTabStrip tabs = (PagerTabStrip) rootView.findViewById(R.id.tabs);
@@ -175,16 +175,19 @@ public class AppOpsSummary extends InstrumentedFragment {
        return rootView;
    }

    private AppOpsState.OpsTemplate[] filterTemplates(AppOpsState.OpsTemplate[] templates) {
        List<AppOpsState.OpsTemplate> validTemplates = new ArrayList(templates.length);
        for (AppOpsState.OpsTemplate template : templates) {
            if (template == AppOpsState.SU_TEMPLATE
                    && !DevelopmentSettings.isRootForAppsEnabled()) {
                continue;
    private void filterPageData(List<Pair<CharSequence, AppOpsState.OpsTemplate>> data, int tab) {
        if (tab >= 0 && tab < data.size()) {
            Pair<CharSequence, AppOpsState.OpsTemplate> item = data.get(tab);
            data.clear();
            data.add(item);
        } else if (!DevelopmentSettings.isRootForAppsEnabled()) {
            for (Pair<CharSequence, AppOpsState.OpsTemplate> item : data) {
                if (item.second == AppOpsState.SU_TEMPLATE) {
                    data.remove(item);
                    return;
                }
            }
            validTemplates.add(template);
        }
        return validTemplates.toArray(new AppOpsState.OpsTemplate[0]);
    }

    private boolean shouldShowUserApps() {