From 8ebe1736d53e80b55f862ab73427cf0f295d0d37 Mon Sep 17 00:00:00 2001 From: Danny Baumann Date: Mon, 5 Sep 2016 09:14:26 +0200 Subject: 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 --- .../settings/applications/AppOpsSummary.java | 63 +++++++++++----------- 1 file changed, 33 insertions(+), 30 deletions(-) (limited to 'src/com/android') diff --git a/src/com/android/settings/applications/AppOpsSummary.java b/src/com/android/settings/applications/AppOpsSummary.java index faefb65..4ec390d 100644 --- a/src/com/android/settings/applications/AppOpsSummary.java +++ b/src/com/android/settings/applications/AppOpsSummary.java @@ -32,6 +32,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 MetricsLogger.APP_OPS_SUMMARY; } - class MyPagerAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener { - private AppOpsState.OpsTemplate[] mPageTemplates; + static class MyPagerAdapter extends FragmentPagerAdapter + implements ViewPager.OnPageChangeListener { + private List> mPageData; + private int mCurPos; - public MyPagerAdapter(FragmentManager fm, AppOpsState.OpsTemplate[] templates) { + public MyPagerAdapter(FragmentManager fm, + List> 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> 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); @@ -174,16 +174,19 @@ public class AppOpsSummary extends InstrumentedFragment { return rootView; } - private AppOpsState.OpsTemplate[] filterTemplates(AppOpsState.OpsTemplate[] templates) { - List validTemplates = new ArrayList(templates.length); - for (AppOpsState.OpsTemplate template : templates) { - if (template == AppOpsState.SU_TEMPLATE - && !DevelopmentSettings.isRootForAppsEnabled()) { - continue; + private void filterPageData(List> data, int tab) { + if (tab >= 0 && tab < data.size()) { + Pair item = data.get(tab); + data.clear(); + data.add(item); + } else if (!DevelopmentSettings.isRootForAppsEnabled()) { + for (Pair 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() { -- cgit v1.1