1 /*
2  * Copyright (C) 2019 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.overlay;
18 
19 import android.app.Fragment;
20 import android.os.Bundle;
21 import android.util.Log;
22 
23 import androidx.annotation.Keep;
24 
25 import com.android.tv.settings.BaseSettingsFragment;
26 import com.android.tv.settings.SettingsFragmentProvider;
27 
28 /**
29  * Default implementation of the feature factory.
30  */
31 @Keep
32 public class FeatureFactoryImpl extends FeatureFactory {
33 
34     @Override
getSettingsFragmentProvider()35     public SettingsFragmentProvider getSettingsFragmentProvider() {
36         return SettingsFragment::newInstance;
37     }
38 
39     @Override
isTwoPanelLayout()40     public boolean isTwoPanelLayout() {
41         return false;
42     }
43 
44     /** A settings fragment suitable for displaying in the default (one panel) layout. */
45     public static class SettingsFragment extends BaseSettingsFragment {
46 
SettingsFragment()47         public SettingsFragment() {}
48 
49         /** Constructs a new instance of a settings fragment. */
newInstance(String className, Bundle arguments)50         public static SettingsFragment newInstance(String className, Bundle arguments) {
51             SettingsFragment fragment = new SettingsFragment();
52             Bundle args = arguments == null ? new Bundle() : new Bundle(arguments);
53             args.putString(EXTRA_FRAGMENT_CLASS_NAME, className);
54             fragment.setArguments(args);
55             return fragment;
56         }
57 
58         @Override
onPreferenceStartInitialScreen()59         public void onPreferenceStartInitialScreen() {
60             try {
61                 String className = getArguments().getString(EXTRA_FRAGMENT_CLASS_NAME);
62                 final Fragment fragment = (Fragment) Class.forName(className).newInstance();
63                 fragment.setArguments(getArguments());
64                 startPreferenceFragment(fragment);
65             } catch (IllegalAccessException | ClassNotFoundException
66                     | java.lang.InstantiationException e) {
67                 Log.e(FeatureFactory.TAG, "Unable to start initial preference screen.", e);
68             }
69         }
70     }
71 }
72