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 package com.android.dialer.app.filterednumber;
17 
18 import android.app.ListFragment;
19 import android.app.LoaderManager;
20 import android.content.Context;
21 import android.content.CursorLoader;
22 import android.content.Loader;
23 import android.database.Cursor;
24 import android.graphics.drawable.ColorDrawable;
25 import android.os.Bundle;
26 import android.support.v7.app.ActionBar;
27 import android.support.v7.app.AppCompatActivity;
28 import android.view.LayoutInflater;
29 import android.view.View;
30 import android.view.ViewGroup;
31 import android.widget.TextView;
32 import com.android.dialer.app.R;
33 import com.android.dialer.blocking.BlockedNumbersMigrator;
34 import com.android.dialer.blocking.BlockedNumbersMigrator.Listener;
35 import com.android.dialer.blocking.FilteredNumberCompat;
36 import com.android.dialer.blocking.FilteredNumbersUtil;
37 import com.android.dialer.blocking.FilteredNumbersUtil.CheckForSendToVoicemailContactListener;
38 import com.android.dialer.blocking.FilteredNumbersUtil.ImportSendToVoicemailContactsListener;
39 import com.android.dialer.database.FilteredNumberContract;
40 import com.android.dialer.lettertile.LetterTileDrawable;
41 import com.android.dialer.theme.base.ThemeComponent;
42 import com.android.dialer.voicemailstatus.VisualVoicemailEnabledChecker;
43 
44 /** TODO(calderwoodra): documentation */
45 public class BlockedNumbersFragment extends ListFragment
46     implements LoaderManager.LoaderCallbacks<Cursor>,
47         View.OnClickListener,
48         VisualVoicemailEnabledChecker.Callback {
49 
50   private static final char ADD_BLOCKED_NUMBER_ICON_LETTER = '+';
51   protected View migratePromoView;
52   private BlockedNumbersMigrator blockedNumbersMigratorForTest;
53   private TextView blockedNumbersText;
54   private TextView footerText;
55   private BlockedNumbersAdapter adapter;
56   private VisualVoicemailEnabledChecker voicemailEnabledChecker;
57   private View importSettings;
58   private View blockedNumbersDisabledForEmergency;
59   private View blockedNumberListDivider;
60 
61   @Override
getContext()62   public Context getContext() {
63     return getActivity();
64   }
65 
66   @Override
onActivityCreated(Bundle savedInstanceState)67   public void onActivityCreated(Bundle savedInstanceState) {
68     super.onActivityCreated(savedInstanceState);
69 
70     LayoutInflater inflater =
71         (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
72     getListView().addHeaderView(inflater.inflate(R.layout.blocked_number_header, null));
73     getListView().addFooterView(inflater.inflate(R.layout.blocked_number_footer, null));
74     //replace the icon for add number with LetterTileDrawable(), so it will have identical style
75     LetterTileDrawable drawable = new LetterTileDrawable(getResources());
76     drawable.setLetter(ADD_BLOCKED_NUMBER_ICON_LETTER);
77     drawable.setColor(ThemeComponent.get(getContext()).theme().getColorIcon());
78     drawable.setIsCircular(true);
79 
80     if (adapter == null) {
81       adapter =
82           BlockedNumbersAdapter.newBlockedNumbersAdapter(
83               getContext(), getActivity().getFragmentManager());
84     }
85     setListAdapter(adapter);
86 
87     blockedNumbersText = (TextView) getListView().findViewById(R.id.blocked_number_text_view);
88     migratePromoView = getListView().findViewById(R.id.migrate_promo);
89     getListView().findViewById(R.id.migrate_promo_allow_button).setOnClickListener(this);
90     importSettings = getListView().findViewById(R.id.import_settings);
91     blockedNumbersDisabledForEmergency =
92         getListView().findViewById(R.id.blocked_numbers_disabled_for_emergency);
93     blockedNumberListDivider = getActivity().findViewById(R.id.blocked_number_list_divider);
94     getListView().findViewById(R.id.import_button).setOnClickListener(this);
95     getListView().findViewById(R.id.view_numbers_button).setOnClickListener(this);
96 
97     footerText = (TextView) getActivity().findViewById(R.id.blocked_number_footer_textview);
98     voicemailEnabledChecker = new VisualVoicemailEnabledChecker(getContext(), this);
99     voicemailEnabledChecker.asyncUpdate();
100     updateActiveVoicemailProvider();
101   }
102 
103   @Override
onDestroy()104   public void onDestroy() {
105     setListAdapter(null);
106     super.onDestroy();
107   }
108 
109   @Override
onCreate(Bundle savedInstanceState)110   public void onCreate(Bundle savedInstanceState) {
111     super.onCreate(savedInstanceState);
112     getLoaderManager().initLoader(0, null, this);
113   }
114 
115   @Override
onResume()116   public void onResume() {
117     super.onResume();
118 
119     ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
120     ColorDrawable backgroundDrawable =
121         new ColorDrawable(ThemeComponent.get(getContext()).theme().getColorPrimary());
122     actionBar.setBackgroundDrawable(backgroundDrawable);
123     actionBar.setDisplayShowCustomEnabled(false);
124     actionBar.setDisplayHomeAsUpEnabled(true);
125     actionBar.setDisplayShowHomeEnabled(true);
126     actionBar.setDisplayShowTitleEnabled(true);
127     actionBar.setTitle(R.string.manage_blocked_numbers_label);
128 
129     // If the device can use the framework blocking solution, users should not be able to add
130     // new blocked numbers from the Blocked Management UI. They will be shown a promo card
131     // asking them to migrate to new blocking instead.
132     if (FilteredNumberCompat.canUseNewFiltering()) {
133       migratePromoView.setVisibility(View.VISIBLE);
134       blockedNumbersText.setVisibility(View.GONE);
135       blockedNumberListDivider.setVisibility(View.GONE);
136       importSettings.setVisibility(View.GONE);
137       getListView().findViewById(R.id.import_button).setOnClickListener(null);
138       getListView().findViewById(R.id.view_numbers_button).setOnClickListener(null);
139       blockedNumbersDisabledForEmergency.setVisibility(View.GONE);
140       footerText.setVisibility(View.GONE);
141     } else {
142       FilteredNumbersUtil.checkForSendToVoicemailContact(
143           getActivity(),
144           new CheckForSendToVoicemailContactListener() {
145             @Override
146             public void onComplete(boolean hasSendToVoicemailContact) {
147               final int visibility = hasSendToVoicemailContact ? View.VISIBLE : View.GONE;
148               importSettings.setVisibility(visibility);
149             }
150           });
151     }
152 
153     // All views except migrate and the block list are hidden when new filtering is available
154     if (!FilteredNumberCompat.canUseNewFiltering()
155         && FilteredNumbersUtil.hasRecentEmergencyCall(getContext())) {
156       blockedNumbersDisabledForEmergency.setVisibility(View.VISIBLE);
157     } else {
158       blockedNumbersDisabledForEmergency.setVisibility(View.GONE);
159     }
160 
161     voicemailEnabledChecker.asyncUpdate();
162   }
163 
164   @Override
onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)165   public View onCreateView(
166       LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
167     return inflater.inflate(R.layout.blocked_number_fragment, container, false);
168   }
169 
170   @Override
onCreateLoader(int id, Bundle args)171   public Loader<Cursor> onCreateLoader(int id, Bundle args) {
172     final String[] projection = {
173       FilteredNumberContract.FilteredNumberColumns._ID,
174       FilteredNumberContract.FilteredNumberColumns.COUNTRY_ISO,
175       FilteredNumberContract.FilteredNumberColumns.NUMBER,
176       FilteredNumberContract.FilteredNumberColumns.NORMALIZED_NUMBER
177     };
178     final String selection =
179         FilteredNumberContract.FilteredNumberColumns.TYPE
180             + "="
181             + FilteredNumberContract.FilteredNumberTypes.BLOCKED_NUMBER;
182     return new CursorLoader(
183         getContext(),
184         FilteredNumberContract.FilteredNumber.CONTENT_URI,
185         projection,
186         selection,
187         null,
188         null);
189   }
190 
191   @Override
onLoadFinished(Loader<Cursor> loader, Cursor data)192   public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
193     adapter.swapCursor(data);
194     if (FilteredNumberCompat.canUseNewFiltering() || data.getCount() == 0) {
195       blockedNumberListDivider.setVisibility(View.INVISIBLE);
196     } else {
197       blockedNumberListDivider.setVisibility(View.VISIBLE);
198     }
199   }
200 
201   @Override
onLoaderReset(Loader<Cursor> loader)202   public void onLoaderReset(Loader<Cursor> loader) {
203     adapter.swapCursor(null);
204   }
205 
206   @Override
onClick(final View view)207   public void onClick(final View view) {
208     final BlockedNumbersSettingsActivity activity = (BlockedNumbersSettingsActivity) getActivity();
209     if (activity == null) {
210       return;
211     }
212 
213     int resId = view.getId();
214     if (resId == R.id.view_numbers_button) {
215       activity.showNumbersToImportPreviewUi();
216     } else if (resId == R.id.import_button) {
217       FilteredNumbersUtil.importSendToVoicemailContacts(
218           activity,
219           new ImportSendToVoicemailContactsListener() {
220             @Override
221             public void onImportComplete() {
222               importSettings.setVisibility(View.GONE);
223             }
224           });
225     } else if (resId == R.id.migrate_promo_allow_button) {
226       view.setEnabled(false);
227       (blockedNumbersMigratorForTest != null
228               ? blockedNumbersMigratorForTest
229               : new BlockedNumbersMigrator(getContext()))
230           .migrate(
231               new Listener() {
232                 @Override
233                 public void onComplete() {
234                   getContext()
235                       .startActivity(
236                           FilteredNumberCompat.createManageBlockedNumbersIntent(getContext()));
237                   // Remove this activity from the backstack
238                   activity.finish();
239                 }
240               });
241     }
242   }
243 
244   @Override
onVisualVoicemailEnabledStatusChanged(boolean newStatus)245   public void onVisualVoicemailEnabledStatusChanged(boolean newStatus) {
246     updateActiveVoicemailProvider();
247   }
248 
updateActiveVoicemailProvider()249   private void updateActiveVoicemailProvider() {
250     if (getActivity() == null || getActivity().isFinishing()) {
251       return;
252     }
253     if (voicemailEnabledChecker.isVisualVoicemailEnabled()) {
254       footerText.setText(R.string.block_number_footer_message_vvm);
255     } else {
256       footerText.setText(R.string.block_number_footer_message_no_vvm);
257     }
258   }
259 
setBlockedNumbersMigratorForTest(BlockedNumbersMigrator blockedNumbersMigrator)260   void setBlockedNumbersMigratorForTest(BlockedNumbersMigrator blockedNumbersMigrator) {
261     blockedNumbersMigratorForTest = blockedNumbersMigrator;
262   }
263 }
264