Home | History | Annotate | Download | only in tuner
      1 /*
      2  * Copyright (C) 2015 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 package com.android.systemui.tuner;
     17 
     18 import android.app.Fragment;
     19 import android.app.FragmentTransaction;
     20 import android.os.Bundle;
     21 import android.support.v14.preference.PreferenceFragment;
     22 import android.support.v7.preference.Preference;
     23 import android.support.v7.preference.PreferenceScreen;
     24 import android.util.Log;
     25 import android.view.MenuItem;
     26 
     27 import com.android.settingslib.drawer.SettingsDrawerActivity;
     28 import com.android.systemui.Dependency;
     29 import com.android.systemui.R;
     30 import com.android.systemui.fragments.FragmentService;
     31 
     32 public class TunerActivity extends SettingsDrawerActivity implements
     33         PreferenceFragment.OnPreferenceStartFragmentCallback,
     34         PreferenceFragment.OnPreferenceStartScreenCallback {
     35 
     36     private static final String TAG_TUNER = "tuner";
     37 
     38     protected void onCreate(Bundle savedInstanceState) {
     39         super.onCreate(savedInstanceState);
     40         Dependency.initDependencies(this);
     41 
     42         if (getFragmentManager().findFragmentByTag(TAG_TUNER) == null) {
     43             final String action = getIntent().getAction();
     44             boolean showDemoMode = action != null && action.equals(
     45                     "com.android.settings.action.DEMO_MODE");
     46             final PreferenceFragment fragment = showDemoMode ? new DemoModeFragment()
     47                     : new TunerFragment();
     48             getFragmentManager().beginTransaction().replace(R.id.content_frame,
     49                     fragment, TAG_TUNER).commit();
     50         }
     51     }
     52 
     53     @Override
     54     protected void onDestroy() {
     55         super.onDestroy();
     56         Dependency.destroy(FragmentService.class, s -> s.destroyAll());
     57         Dependency.clearDependencies();
     58     }
     59 
     60     @Override
     61     public boolean onMenuItemSelected(int featureId, MenuItem item) {
     62         if (item.getItemId() == android.R.id.home) {
     63             onBackPressed();
     64             return true;
     65         }
     66         return super.onMenuItemSelected(featureId, item);
     67     }
     68 
     69     @Override
     70     public void onBackPressed() {
     71         if (!getFragmentManager().popBackStackImmediate()) {
     72             super.onBackPressed();
     73         }
     74     }
     75 
     76     @Override
     77     public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
     78         try {
     79             Class<?> cls = Class.forName(pref.getFragment());
     80             Fragment fragment = (Fragment) cls.newInstance();
     81             final Bundle b = new Bundle(1);
     82             b.putString(PreferenceFragment.ARG_PREFERENCE_ROOT, pref.getKey());
     83             fragment.setArguments(b);
     84             FragmentTransaction transaction = getFragmentManager().beginTransaction();
     85             setTitle(pref.getTitle());
     86             transaction.replace(R.id.content_frame, fragment);
     87             transaction.addToBackStack("PreferenceFragment");
     88             transaction.commit();
     89             return true;
     90         } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
     91             Log.d("TunerActivity", "Problem launching fragment", e);
     92             return false;
     93         }
     94     }
     95 
     96     @Override
     97     public boolean onPreferenceStartScreen(PreferenceFragment caller, PreferenceScreen pref) {
     98         FragmentTransaction transaction = getFragmentManager().beginTransaction();
     99         SubSettingsFragment fragment = new SubSettingsFragment();
    100         final Bundle b = new Bundle(1);
    101         b.putString(PreferenceFragment.ARG_PREFERENCE_ROOT, pref.getKey());
    102         fragment.setArguments(b);
    103         fragment.setTargetFragment(caller, 0);
    104         transaction.replace(R.id.content_frame, fragment);
    105         transaction.addToBackStack("PreferenceFragment");
    106         transaction.commit();
    107         return true;
    108     }
    109 
    110     public static class SubSettingsFragment extends PreferenceFragment {
    111         private PreferenceScreen mParentScreen;
    112 
    113         @Override
    114         public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    115             mParentScreen =
    116                     (PreferenceScreen) ((PreferenceFragment) getTargetFragment())
    117                             .getPreferenceScreen().findPreference(rootKey);
    118             PreferenceScreen screen =
    119                     getPreferenceManager().createPreferenceScreen(
    120                             getPreferenceManager().getContext());
    121             setPreferenceScreen(screen);
    122             // Copy all the preferences over to this screen so they go into the attached state.
    123             while (mParentScreen.getPreferenceCount() > 0) {
    124                 Preference p = mParentScreen.getPreference(0);
    125                 mParentScreen.removePreference(p);
    126                 screen.addPreference(p);
    127             }
    128         }
    129 
    130         @Override
    131         public void onDestroy() {
    132             super.onDestroy();
    133             // Copy all the preferences back so we don't lose them.
    134             PreferenceScreen screen = getPreferenceScreen();
    135             while (screen.getPreferenceCount() > 0) {
    136                 Preference p = screen.getPreference(0);
    137                 screen.removePreference(p);
    138                 mParentScreen.addPreference(p);
    139             }
    140         }
    141     }
    142 
    143 }
    144