• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.car.settings.common;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.os.Bundle;
22 
23 import androidx.annotation.NonNull;
24 import androidx.annotation.Nullable;
25 import androidx.fragment.app.Fragment;
26 
27 /**
28  * Activity class for launching a SubSetting as a separate activity.
29  */
30 public class SubSettingsActivity extends BaseCarSettingsActivity {
31     public static final String KEY_SUB_SETTINGS_FRAGMENT = "key_sub_settings_fragment";
32     public static final String KEY_SUB_SETTINGS_FRAGMENT_ARGS = "key_sub_settings_fragment_args";
33     private static final Logger LOG = new Logger(SubSettingsActivity.class);
34 
35     /**
36      * Create a new SubSettingsActivity instance for the given fragment.
37      */
newInstance(@onNull Context context, @NonNull Fragment fragment)38     public static Intent newInstance(@NonNull Context context, @NonNull Fragment fragment) {
39         String fragmentClass = fragment.getClass().getName();
40         Intent intent = new Intent(context, SubSettingsActivity.class);
41         intent.putExtra(KEY_SUB_SETTINGS_FRAGMENT, fragmentClass);
42         intent.putExtra(KEY_SUB_SETTINGS_FRAGMENT_ARGS, fragment.getArguments());
43         return intent;
44     }
45 
46     @Nullable
47     @Override
getInitialFragment()48     protected Fragment getInitialFragment() {
49         try {
50             String fragmentClass = getIntent().getStringExtra(KEY_SUB_SETTINGS_FRAGMENT);
51             Bundle fragmentArgs = getIntent().getBundleExtra(KEY_SUB_SETTINGS_FRAGMENT_ARGS);
52             Fragment fragment = getSupportFragmentManager().getFragmentFactory().instantiate(
53                     getClassLoader(), fragmentClass);
54             fragment.setArguments(fragmentArgs);
55             return fragment;
56         } catch (Fragment.InstantiationException e) {
57             LOG.e("Unable to instantiate fragment", e);
58             return null;
59         }
60     }
61 }
62