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.handheld;
18 
19 import android.os.Bundle;
20 import android.preference.PreferenceFragment;
21 import android.view.LayoutInflater;
22 import android.view.View;
23 import android.view.ViewGroup;
24 import android.view.animation.Animation;
25 import android.view.animation.Animation.AnimationListener;
26 import android.view.animation.AnimationUtils;
27 import android.widget.ListView;
28 import android.widget.TextView;
29 import com.android.packageinstaller.R;
30 
31 public abstract class PermissionsFrameFragment extends PreferenceFragment {
32     private ViewGroup mPreferencesContainer;
33 
34     private View mLoadingView;
35     private ViewGroup mPrefsView;
36     private boolean mIsLoading;
37 
38     /**
39      * Returns the view group that holds the preferences objects. This will
40      * only be set after {@link #onCreateView} has been called.
41      */
getPreferencesContainer()42     protected final ViewGroup getPreferencesContainer() {
43         return mPreferencesContainer;
44     }
45 
46     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)47     public View onCreateView(LayoutInflater inflater, ViewGroup container,
48             Bundle savedInstanceState) {
49         ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.permissions_frame, container,
50                         false);
51         mPrefsView = (ViewGroup) rootView.findViewById(R.id.prefs_container);
52         if (mPrefsView == null) {
53             mPrefsView = rootView;
54         }
55         mLoadingView = rootView.findViewById(R.id.loading_container);
56         mPreferencesContainer = (ViewGroup) super.onCreateView(
57                 inflater, mPrefsView, savedInstanceState);
58         setLoading(mIsLoading, false, true /* force */);
59         mPrefsView.addView(mPreferencesContainer);
60         return rootView;
61     }
62 
setLoading(boolean loading, boolean animate)63     protected void setLoading(boolean loading, boolean animate) {
64         setLoading(loading, animate, false);
65     }
66 
setLoading(boolean loading, boolean animate, boolean force)67     private void setLoading(boolean loading, boolean animate, boolean force) {
68         if (mIsLoading != loading || force) {
69             mIsLoading = loading;
70             if (getView() == null) {
71                 // If there is no created view, there is no reason to animate.
72                 animate = false;
73             }
74             if (mPrefsView != null) {
75                 setViewShown(mPrefsView, !loading, animate);
76             }
77             if (mLoadingView != null) {
78                 setViewShown(mLoadingView, loading, animate);
79             }
80         }
81     }
82 
83     @Override
getListView()84     public ListView getListView() {
85         ListView listView = super.getListView();
86         if (listView.getEmptyView() == null) {
87             TextView emptyView = (TextView) getView().findViewById(R.id.no_permissions);
88             listView.setEmptyView(emptyView);
89         }
90         return listView;
91     }
92 
setViewShown(final View view, boolean shown, boolean animate)93     private void setViewShown(final View view, boolean shown, boolean animate) {
94         if (animate) {
95             Animation animation = AnimationUtils.loadAnimation(getContext(),
96                     shown ? android.R.anim.fade_in : android.R.anim.fade_out);
97             if (shown) {
98                 view.setVisibility(View.VISIBLE);
99             } else {
100                 animation.setAnimationListener(new AnimationListener() {
101                     @Override
102                     public void onAnimationStart(Animation animation) {
103                     }
104 
105                     @Override
106                     public void onAnimationRepeat(Animation animation) {
107                     }
108 
109                     @Override
110                     public void onAnimationEnd(Animation animation) {
111                         view.setVisibility(View.INVISIBLE);
112                     }
113                 });
114             }
115             view.startAnimation(animation);
116         } else {
117             view.clearAnimation();
118             view.setVisibility(shown ? View.VISIBLE : View.INVISIBLE);
119         }
120     }
121 }
122