• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy 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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.tv.settings;
18 
19 import com.android.tv.settings.dialog.old.Action;
20 import com.android.tv.settings.dialog.old.ActionAdapter;
21 import com.android.tv.settings.dialog.old.ActionFragment;
22 import com.android.tv.settings.dialog.old.ContentFragment;
23 import com.android.tv.settings.dialog.old.DialogActivity;
24 
25 import android.app.Fragment;
26 import android.content.res.Resources;
27 import android.net.Uri;
28 import android.os.Bundle;
29 import android.view.KeyEvent;
30 
31 import java.util.ArrayList;
32 import java.util.Stack;
33 
34 public abstract class BaseSettingsActivity extends DialogActivity {
35 
36     protected Object mState;
37     protected Stack<Object> mStateStack = new Stack<Object>();
38     protected Resources mResources;
39     protected Fragment mContentFragment;
40     protected Fragment mActionFragment;
41     protected ArrayList<Action> mActions;
42 
43     /**
44      * This method initializes the parameter and sets the initial state. <br/>
45      * Activities extending {@link BaseSettingsActivity} should initialize their
46      * own local variables, if any, before calling {@link #onCreate}
47      */
48     @Override
onCreate(Bundle savedInstanceState)49     protected void onCreate(Bundle savedInstanceState) {
50         mResources = getResources();
51         mActions = new ArrayList<Action>();
52         super.onCreate(savedInstanceState);
53 
54         setState(getInitialState(), true);
55     }
56 
getInitialState()57     protected abstract Object getInitialState();
58 
setState(Object state, boolean updateStateStack)59     protected void setState(Object state, boolean updateStateStack) {
60         if (updateStateStack && mState != null) {
61             mStateStack.push(mState);
62         }
63         mState = state;
64 
65         updateView();
66     }
67 
setView(int titleResId, int breadcrumbResId, int descResId, int iconResId)68     protected void setView(int titleResId, int breadcrumbResId, int descResId, int iconResId) {
69         String title = titleResId != 0 ? mResources.getString(titleResId) : null;
70         String breadcrumb = breadcrumbResId != 0 ? mResources.getString(breadcrumbResId) : null;
71         String description = descResId != 0 ? mResources.getString(descResId) : null;
72         setView(title, breadcrumb, description, iconResId);
73     }
74 
setView(String title, String breadcrumb, String description, int iconResId)75     protected void setView(String title, String breadcrumb, String description, int iconResId) {
76         mContentFragment = ContentFragment.newInstance(title, breadcrumb, description, iconResId,
77                 getResources().getColor(R.color.icon_background));
78         mActionFragment = ActionFragment.newInstance(mActions);
79         setContentAndActionFragments(mContentFragment, mActionFragment);
80     }
81 
82     /**
83      * Set the view.
84      *
85      * @param uri Uri of icon resource.
86      */
setView(String title, String breadcrumb, String description, Uri uri)87     protected void setView(String title, String breadcrumb, String description, Uri uri) {
88         mContentFragment = ContentFragment.newInstance(title, breadcrumb, null, uri,
89                 getResources().getColor(R.color.icon_background));
90         mActionFragment = ActionFragment.newInstance(mActions);
91         setContentAndActionFragments(mContentFragment, mActionFragment);
92     }
93 
setView(int titleResId, String breadcrumb, int descResId, Uri uri)94     protected void setView(int titleResId, String breadcrumb, int descResId, Uri uri) {
95         String title = titleResId != 0 ? mResources.getString(titleResId) : null;
96         String description = descResId != 0 ? mResources.getString(descResId) : null;
97         setView(title, breadcrumb, description, uri);
98     }
99 
100     /**
101      * This method is called by {@link #setState}, and updates the layout based
102      * on the current state
103      */
updateView()104     protected abstract void updateView();
105 
106     /**
107      * This method is called to update the contents of mActions to reflect the
108      * list of actions for the current state.
109      */
refreshActionList()110     protected abstract void refreshActionList();
111 
112     /**
113      * This method is used to set boolean properties
114      *
115      * @param enable whether or not to enable the property
116      */
setProperty(boolean enable)117     protected abstract void setProperty(boolean enable);
118 
119     @Override
onKeyDown(int keyCode, KeyEvent event)120     public boolean onKeyDown(int keyCode, KeyEvent event) {
121         if (keyCode == KeyEvent.KEYCODE_BACK) {
122             goBack();
123             return true;
124         }
125         return super.onKeyDown(keyCode, event);
126     }
127 
goBack()128     protected void goBack(){
129         if (mState.equals(getInitialState())) {
130             finish();
131         } else if (getPrevState() != null) {
132             mState = mStateStack.pop();
133             // Using the synchronous version of popBackStack so that we can get
134             // the updated
135             // instance of the action Fragment on the following line.
136             getFragmentManager().popBackStackImmediate();
137             mActionFragment = getActionFragment();
138             // Update Current State Actions
139             if ((mActionFragment != null) && (mActionFragment instanceof ActionFragment)) {
140                 ActionFragment actFrag = (ActionFragment) mActionFragment;
141                 refreshActionList();
142                 ((ActionAdapter) actFrag.getAdapter()).setActions(mActions);
143             }
144         }
145     }
146 
getPrevState()147     protected Object getPrevState() {
148         return mStateStack.isEmpty() ? null : mStateStack.peek();
149     }
150 }
151