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 17 package com.android.packageinstaller.permission.ui.television; 18 19 import android.os.Bundle; 20 import android.support.annotation.Nullable; 21 import android.support.v14.preference.PreferenceFragment; 22 import android.support.v17.leanback.widget.VerticalGridView; 23 import android.support.v7.preference.PreferenceScreen; 24 import android.support.v7.widget.RecyclerView; 25 import android.view.LayoutInflater; 26 import android.view.View; 27 import android.view.ViewGroup; 28 import android.view.animation.Animation; 29 import android.view.animation.Animation.AnimationListener; 30 import android.view.animation.AnimationUtils; 31 import android.widget.TextView; 32 33 import com.android.packageinstaller.R; 34 35 public abstract class PermissionsFrameFragment extends PreferenceFragment { 36 37 // Key identifying the preference used on TV as the extra header in a permission fragment. 38 // This is to distinguish it from the rest of the preferences 39 protected static final String HEADER_PREFERENCE_KEY = "HeaderPreferenceKey"; 40 41 private ViewGroup mPreferencesContainer; 42 43 // TV-specific instance variable 44 @Nullable private RecyclerView mGridView; 45 46 private View mLoadingView; 47 private ViewGroup mPrefsView; 48 private boolean mIsLoading; 49 50 /** 51 * Returns the view group that holds the preferences objects. This will 52 * only be set after {@link #onCreateView} has been called. 53 */ getPreferencesContainer()54 protected final ViewGroup getPreferencesContainer() { 55 return mPreferencesContainer; 56 } 57 58 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)59 public View onCreateView(LayoutInflater inflater, ViewGroup container, 60 Bundle savedInstanceState) { 61 ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.permissions_frame, container, 62 false); 63 mPrefsView = (ViewGroup) rootView.findViewById(R.id.prefs_container); 64 if (mPrefsView == null) { 65 mPrefsView = rootView; 66 } 67 mLoadingView = rootView.findViewById(R.id.loading_container); 68 mPreferencesContainer = (ViewGroup) super.onCreateView( 69 inflater, mPrefsView, savedInstanceState); 70 setLoading(mIsLoading, false, true /* force */); 71 mPrefsView.addView(mPreferencesContainer); 72 return rootView; 73 } 74 75 @Override onCreatePreferences(Bundle savedInstanceState, String rootKey)76 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { 77 PreferenceScreen preferences = getPreferenceScreen(); 78 if (preferences == null) { 79 preferences = getPreferenceManager().createPreferenceScreen(getActivity()); 80 setPreferenceScreen(preferences); 81 } 82 } 83 setLoading(boolean loading, boolean animate)84 protected void setLoading(boolean loading, boolean animate) { 85 setLoading(loading, animate, false); 86 } 87 setLoading(boolean loading, boolean animate, boolean force)88 private void setLoading(boolean loading, boolean animate, boolean force) { 89 if (mIsLoading != loading || force) { 90 mIsLoading = loading; 91 if (getView() == null) { 92 // If there is no created view, there is no reason to animate. 93 animate = false; 94 } 95 if (mPrefsView != null) { 96 setViewShown(mPrefsView, !loading, animate); 97 } 98 if (mLoadingView != null) { 99 setViewShown(mLoadingView, loading, animate); 100 } 101 } 102 } 103 setViewShown(final View view, boolean shown, boolean animate)104 private void setViewShown(final View view, boolean shown, boolean animate) { 105 if (animate) { 106 Animation animation = AnimationUtils.loadAnimation(getContext(), 107 shown ? android.R.anim.fade_in : android.R.anim.fade_out); 108 if (shown) { 109 view.setVisibility(View.VISIBLE); 110 } else { 111 animation.setAnimationListener(new AnimationListener() { 112 @Override 113 public void onAnimationStart(Animation animation) { 114 } 115 116 @Override 117 public void onAnimationRepeat(Animation animation) { 118 } 119 120 @Override 121 public void onAnimationEnd(Animation animation) { 122 view.setVisibility(View.INVISIBLE); 123 } 124 }); 125 } 126 view.startAnimation(animation); 127 } else { 128 view.clearAnimation(); 129 view.setVisibility(shown ? View.VISIBLE : View.INVISIBLE); 130 } 131 } 132 133 @Override onCreateRecyclerView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)134 public RecyclerView onCreateRecyclerView(LayoutInflater inflater, ViewGroup parent, 135 Bundle savedInstanceState) { 136 VerticalGridView verticalGridView = (VerticalGridView) inflater.inflate( 137 R.layout.leanback_preferences_list, parent, false); 138 verticalGridView.setWindowAlignment(VerticalGridView.WINDOW_ALIGN_BOTH_EDGE); 139 verticalGridView.setFocusScrollStrategy(VerticalGridView.FOCUS_SCROLL_ALIGNED); 140 mGridView = verticalGridView; 141 return mGridView; 142 } 143 144 @Override onCreateAdapter(PreferenceScreen preferenceScreen)145 protected RecyclerView.Adapter<?> onCreateAdapter(PreferenceScreen preferenceScreen) { 146 final RecyclerView.Adapter<?> adapter = super.onCreateAdapter(preferenceScreen); 147 148 if (adapter != null) { 149 final TextView emptyView = (TextView) getView().findViewById(R.id.no_permissions); 150 onSetEmptyText(emptyView); 151 final RecyclerView recyclerView = getListView(); 152 adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() { 153 @Override 154 public void onChanged() { 155 checkEmpty(); 156 } 157 158 @Override 159 public void onItemRangeInserted(int positionStart, int itemCount) { 160 checkEmpty(); 161 } 162 163 @Override 164 public void onItemRangeRemoved(int positionStart, int itemCount) { 165 checkEmpty(); 166 } 167 168 private void checkEmpty() { 169 boolean isEmpty = isPreferenceListEmpty(); 170 emptyView.setVisibility(isEmpty ? View.VISIBLE : View.GONE); 171 recyclerView.setVisibility(isEmpty && adapter.getItemCount() == 0 ? 172 View.GONE : View.VISIBLE); 173 if (!isEmpty && mGridView != null) { 174 mGridView.requestFocus(); 175 } 176 } 177 }); 178 179 boolean isEmpty = isPreferenceListEmpty(); 180 emptyView.setVisibility(isEmpty ? View.VISIBLE : View.GONE); 181 recyclerView.setVisibility(isEmpty && adapter.getItemCount() == 0 ? 182 View.GONE : View.VISIBLE); 183 if (!isEmpty && mGridView != null) { 184 mGridView.requestFocus(); 185 } 186 } 187 188 return adapter; 189 } 190 isPreferenceListEmpty()191 private boolean isPreferenceListEmpty() { 192 PreferenceScreen screen = getPreferenceScreen(); 193 return screen.getPreferenceCount() == 0 || ( 194 screen.getPreferenceCount() == 1 && 195 (screen.findPreference(HEADER_PREFERENCE_KEY) != null)); 196 } 197 198 /** 199 * Hook for subclasses to change the default text of the empty view. 200 * Base implementation leaves the default empty view text. 201 * 202 * @param textView the empty text view 203 */ onSetEmptyText(TextView textView)204 protected void onSetEmptyText(TextView textView) { 205 } 206 } 207