1 /* 2 * Copyright (C) 2017 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.app.Fragment; 20 import android.os.Bundle; 21 import android.support.annotation.LayoutRes; 22 import android.support.annotation.StringRes; 23 import android.support.v7.app.ActionBar; 24 import android.support.v7.app.AppCompatActivity; 25 import android.support.v7.widget.Toolbar; 26 import android.view.LayoutInflater; 27 import android.view.View; 28 import android.view.ViewGroup; 29 import android.widget.TextView; 30 31 import com.android.car.settings.R; 32 33 import java.util.Set; 34 35 /** 36 * Base fragment for setting activity. 37 */ 38 public abstract class BaseFragment extends Fragment { 39 public static final String EXTRA_TITLE_ID = "extra_title_id"; 40 public static final String EXTRA_LAYOUT = "extra_layout"; 41 public static final String EXTRA_ACTION_BAR_LAYOUT = "extra_action_bar_layout"; 42 43 /** 44 * Controls the transition of fragment. 45 */ 46 public interface FragmentController { 47 /** 48 * Launches fragment in the main container of current activity. 49 */ launchFragment(BaseFragment fragment)50 void launchFragment(BaseFragment fragment); 51 52 /** 53 * Pops the top off the fragment stack. 54 */ goBack()55 void goBack(); 56 } 57 58 @LayoutRes 59 protected int mLayout; 60 61 @LayoutRes 62 private int mActionBarLayout; 63 64 @StringRes 65 private int mTitleId; 66 67 protected FragmentController mFragmentController; 68 setFragmentController(FragmentController fragmentController)69 public void setFragmentController(FragmentController fragmentController) { 70 mFragmentController = fragmentController; 71 } 72 getBundle()73 protected static Bundle getBundle() { 74 Bundle bundle = new Bundle(); 75 bundle.putInt(EXTRA_ACTION_BAR_LAYOUT, R.layout.action_bar); 76 return bundle; 77 } 78 79 @Override onCreate(Bundle savedInstanceState)80 public void onCreate(Bundle savedInstanceState) { 81 super.onCreate(savedInstanceState); 82 Set<String> extraKeys = getArguments().keySet(); 83 if (extraKeys.contains(EXTRA_ACTION_BAR_LAYOUT)) { 84 mActionBarLayout = getArguments().getInt(EXTRA_ACTION_BAR_LAYOUT); 85 } else { 86 throw new IllegalArgumentException("must specify a actionBar layout"); 87 } 88 if (extraKeys.contains(EXTRA_LAYOUT)) { 89 mLayout = getArguments().getInt(EXTRA_LAYOUT); 90 } else { 91 throw new IllegalArgumentException("must specify a layout"); 92 } 93 if (extraKeys.contains(EXTRA_TITLE_ID)) { 94 mTitleId = getArguments().getInt(EXTRA_TITLE_ID); 95 } else { 96 throw new IllegalArgumentException("must specify a title"); 97 } 98 } 99 100 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)101 public View onCreateView(LayoutInflater inflater, ViewGroup container, 102 Bundle savedInstanceState) { 103 return inflater.inflate(mLayout, container, false); 104 } 105 106 @Override onActivityCreated(Bundle savedInstanceState)107 public void onActivityCreated(Bundle savedInstanceState) { 108 super.onActivityCreated(savedInstanceState); 109 ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar(); 110 actionBar.setDisplayHomeAsUpEnabled(false); 111 actionBar.setCustomView(mActionBarLayout); 112 actionBar.setDisplayShowCustomEnabled(true); 113 // make the toolbar take the whole width. 114 Toolbar toolbar=(Toolbar)actionBar.getCustomView().getParent(); 115 toolbar.setPadding(0, 0, 0, 0); 116 getActivity().findViewById(R.id.action_bar_icon_container).setOnClickListener( 117 v -> mFragmentController.goBack()); 118 ((TextView) getActivity().findViewById(R.id.title)).setText(mTitleId); 119 } 120 } 121