• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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 package com.android.contacts.list;
17 
18 import android.app.Fragment;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.res.Configuration;
22 import android.graphics.PorterDuff;
23 import android.os.Bundle;
24 import androidx.core.content.ContextCompat;
25 import android.view.Gravity;
26 import android.view.LayoutInflater;
27 import android.view.View;
28 import android.view.View.OnClickListener;
29 import android.view.ViewGroup;
30 import android.widget.Button;
31 import android.widget.ImageView;
32 import android.widget.LinearLayout;
33 import android.widget.ProgressBar;
34 import android.widget.TextView;
35 
36 import com.android.contacts.R;
37 import com.android.contacts.compat.ProviderStatusCompat;
38 import com.android.contacts.interactions.ImportDialogFragment;
39 import com.android.contacts.util.ImplicitIntentsUtil;
40 
41 /**
42  * Fragment shown when contacts are unavailable. It contains provider status
43  * messaging as well as instructions for the user.
44  */
45 public class ContactsUnavailableFragment extends Fragment implements OnClickListener {
46 
47     private View mView;
48     private ImageView mImageView;
49     private TextView mMessageView;
50     private Button mAddAccountButton;
51     private Button mImportContactsButton;
52     private ProgressBar mProgress;
53     private View mButtonsContainer;
54 
55     private Integer mProviderStatus;
56 
57     @Override
onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)58     public View onCreateView(
59             LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
60         mView = inflater.inflate(R.layout.contacts_unavailable_fragment, null);
61 
62         mImageView = (ImageView) mView.findViewById(R.id.empty_image);
63         final LinearLayout.LayoutParams layoutParams =
64                 (LinearLayout.LayoutParams) mImageView.getLayoutParams();
65         final int screenHeight = getResources().getDisplayMetrics().heightPixels;
66         final int topMargin =
67                 screenHeight / getResources()
68                         .getInteger(R.integer.contacts_no_account_empty_image_margin_divisor)
69                 - getResources()
70                         .getDimensionPixelSize(R.dimen.contacts_no_account_empty_image_offset);
71         layoutParams.setMargins(0, topMargin, 0, 0);
72         layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
73         mImageView.setLayoutParams(layoutParams);
74 
75         mMessageView = (TextView) mView.findViewById(R.id.message);
76         mAddAccountButton = (Button) mView.findViewById(R.id.add_account_button);
77         mAddAccountButton.setOnClickListener(this);
78         mAddAccountButton.getBackground().setColorFilter(ContextCompat.getColor(getContext(), R
79                 .color.primary_color), PorterDuff.Mode.SRC_ATOP);
80         mImportContactsButton = (Button) mView.findViewById(R.id.import_contacts_button);
81         mImportContactsButton.setOnClickListener(this);
82         mImportContactsButton.getBackground().setColorFilter(ContextCompat.getColor(getContext(),
83                 R.color.primary_color), PorterDuff.Mode.SRC_ATOP);
84         mProgress = (ProgressBar) mView.findViewById(R.id.progress);
85 
86         if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
87             mButtonsContainer = mView.findViewById(R.id.buttons_container);
88         }
89 
90         if (mProviderStatus != null) {
91             updateStatus(mProviderStatus);
92         }
93 
94         return mView;
95     }
96 
updateStatus(int providerStatus)97     public void updateStatus(int providerStatus) {
98         mProviderStatus = providerStatus;
99         if (mView == null) {
100             // The view hasn't been inflated yet.
101             return;
102         }
103         if (providerStatus == ProviderStatusCompat.STATUS_EMPTY) {
104             updateViewsForEmptyStatus();
105         } else if (providerStatus == ProviderStatusCompat.STATUS_BUSY
106                 || providerStatus == ProviderStatusCompat.STATUS_CHANGING_LOCALE) {
107             updateViewsForBusyStatus();
108         }
109     }
110 
111     /**
112      * Update views in the fragment when provider status is empty.
113      */
updateViewsForEmptyStatus()114     private void updateViewsForEmptyStatus() {
115         mMessageView.setVisibility(View.VISIBLE);
116         updateButtonVisibility(View.VISIBLE);
117         mProgress.setVisibility(View.GONE);
118     }
119 
120     /**
121      * Update views in the fragment when provider status is busy.
122      */
updateViewsForBusyStatus()123     private void updateViewsForBusyStatus() {
124         mMessageView.setVisibility(View.GONE);
125         mImageView.setVisibility(View.GONE);
126         updateButtonVisibility(View.GONE);
127         mProgress.setVisibility(View.VISIBLE);
128     }
129 
130     @Override
onClick(View v)131     public void onClick(View v) {
132         final int id = v.getId();
133         if (id == R.id.add_account_button) {
134             final Intent intent = ImplicitIntentsUtil.getIntentForAddingGoogleAccount();
135             ImplicitIntentsUtil.startActivityOutsideApp(getActivity(), intent);
136 
137         } else if (id == R.id.import_contacts_button) {
138             ImportDialogFragment.show(getFragmentManager());
139 
140         }
141     }
142 
updateButtonVisibility(int visibility)143     private void updateButtonVisibility(int visibility) {
144         if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
145             mAddAccountButton.setVisibility(visibility);
146             mImportContactsButton.setVisibility(visibility);
147             mButtonsContainer.setVisibility(visibility);
148         } else {
149             mAddAccountButton.setVisibility(visibility);
150             mImportContactsButton.setVisibility(visibility);
151         }
152     }
153 
154     @Override
getContext()155     public Context getContext() {
156         return getActivity();
157     }
158 }
159