1 /**
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy
6  * of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  */
16 
17 package com.android.settings.applications;
18 
19 import android.app.Fragment;
20 import android.app.FragmentManager;
21 import android.content.res.TypedArray;
22 import android.os.Bundle;
23 import android.preference.PreferenceFrameLayout;
24 import android.support.v13.app.FragmentPagerAdapter;
25 import android.support.v4.view.PagerTabStrip;
26 import android.support.v4.view.ViewPager;
27 import android.view.LayoutInflater;
28 import android.view.View;
29 import android.view.ViewGroup;
30 
31 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
32 import com.android.settings.core.InstrumentedPreferenceFragment;
33 import com.android.settings.R;
34 
35 public class AppOpsSummary extends InstrumentedPreferenceFragment {
36     // layout inflater object used to inflate views
37     private LayoutInflater mInflater;
38 
39     private ViewGroup mContentContainer;
40     private View mRootView;
41     private ViewPager mViewPager;
42 
43     CharSequence[] mPageNames;
44     static AppOpsState.OpsTemplate[] sPageTemplates = new AppOpsState.OpsTemplate[] {
45         AppOpsState.LOCATION_TEMPLATE,
46         AppOpsState.PERSONAL_TEMPLATE,
47         AppOpsState.MESSAGING_TEMPLATE,
48         AppOpsState.MEDIA_TEMPLATE,
49         AppOpsState.DEVICE_TEMPLATE
50     };
51 
52     int mCurPos;
53 
54     @Override
getMetricsCategory()55     public int getMetricsCategory() {
56         return MetricsEvent.APP_OPS_SUMMARY;
57     }
58 
59     class MyPagerAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener {
60 
MyPagerAdapter(FragmentManager fm)61         public MyPagerAdapter(FragmentManager fm) {
62             super(fm);
63         }
64 
65         @Override
getItem(int position)66         public Fragment getItem(int position) {
67             return new AppOpsCategory(sPageTemplates[position]);
68         }
69 
70         @Override
getCount()71         public int getCount() {
72             return sPageTemplates.length;
73         }
74 
75         @Override
getPageTitle(int position)76         public CharSequence getPageTitle(int position) {
77             return mPageNames[position];
78         }
79 
80         @Override
onPageScrolled(int position, float positionOffset, int positionOffsetPixels)81         public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
82         }
83 
84         @Override
onPageSelected(int position)85         public void onPageSelected(int position) {
86             mCurPos = position;
87         }
88 
89         @Override
onPageScrollStateChanged(int state)90         public void onPageScrollStateChanged(int state) {
91             if (state == ViewPager.SCROLL_STATE_IDLE) {
92                 //updateCurrentTab(mCurPos);
93             }
94         }
95     }
96 
97     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)98     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
99         // initialize the inflater
100         mInflater = inflater;
101 
102         View rootView = mInflater.inflate(R.layout.app_ops_summary,
103                 container, false);
104         mContentContainer = container;
105         mRootView = rootView;
106 
107         mPageNames = getResources().getTextArray(R.array.app_ops_categories);
108 
109         mViewPager = (ViewPager) rootView.findViewById(R.id.pager);
110         MyPagerAdapter adapter = new MyPagerAdapter(getChildFragmentManager());
111         mViewPager.setAdapter(adapter);
112         mViewPager.setOnPageChangeListener(adapter);
113         PagerTabStrip tabs = (PagerTabStrip) rootView.findViewById(R.id.tabs);
114 
115         // This should be set in the XML layout, but PagerTabStrip lives in
116         // support-v4 and doesn't have styleable attributes.
117         final TypedArray ta = tabs.getContext().obtainStyledAttributes(
118                 new int[] { android.R.attr.colorAccent });
119         final int colorAccent = ta.getColor(0, 0);
120         ta.recycle();
121 
122         tabs.setTabIndicatorColorResource(colorAccent);
123 
124         // We have to do this now because PreferenceFrameLayout looks at it
125         // only when the view is added.
126         if (container instanceof PreferenceFrameLayout) {
127             ((PreferenceFrameLayout.LayoutParams) rootView.getLayoutParams()).removeBorders = true;
128         }
129 
130         return rootView;
131     }
132 }
133