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.os.Bundle;
25 import android.provider.ContactsContract.CommonDataKinds.Phone;
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 com.android.dialer.app.R;
32 import com.android.dialer.blocking.FilteredNumbersUtil;
33 import com.android.dialer.blocking.FilteredNumbersUtil.ImportSendToVoicemailContactsListener;
34 
35 /** TODO(calderwoodra): documentation */
36 public class ViewNumbersToImportFragment extends ListFragment
37     implements LoaderManager.LoaderCallbacks<Cursor>, View.OnClickListener {
38 
39   private ViewNumbersToImportAdapter adapter;
40 
41   @Override
getContext()42   public Context getContext() {
43     return getActivity();
44   }
45 
46   @Override
onActivityCreated(Bundle savedInstanceState)47   public void onActivityCreated(Bundle savedInstanceState) {
48     super.onActivityCreated(savedInstanceState);
49 
50     if (adapter == null) {
51       adapter =
52           ViewNumbersToImportAdapter.newViewNumbersToImportAdapter(
53               getContext(), getActivity().getFragmentManager());
54     }
55     setListAdapter(adapter);
56   }
57 
58   @Override
onDestroy()59   public void onDestroy() {
60     setListAdapter(null);
61     super.onDestroy();
62   }
63 
64   @Override
onCreate(Bundle savedInstanceState)65   public void onCreate(Bundle savedInstanceState) {
66     super.onCreate(savedInstanceState);
67     getLoaderManager().initLoader(0, null, this);
68   }
69 
70   @Override
onResume()71   public void onResume() {
72     super.onResume();
73 
74     ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
75     actionBar.setTitle(R.string.import_send_to_voicemail_numbers_label);
76     actionBar.setDisplayShowCustomEnabled(false);
77     actionBar.setDisplayHomeAsUpEnabled(true);
78     actionBar.setDisplayShowHomeEnabled(true);
79     actionBar.setDisplayShowTitleEnabled(true);
80 
81     getActivity().findViewById(R.id.cancel_button).setOnClickListener(this);
82     getActivity().findViewById(R.id.import_button).setOnClickListener(this);
83   }
84 
85   @Override
onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)86   public View onCreateView(
87       LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
88     return inflater.inflate(R.layout.view_numbers_to_import_fragment, container, false);
89   }
90 
91   @Override
onCreateLoader(int id, Bundle args)92   public Loader<Cursor> onCreateLoader(int id, Bundle args) {
93     final CursorLoader cursorLoader =
94         new CursorLoader(
95             getContext(),
96             Phone.CONTENT_URI,
97             FilteredNumbersUtil.PhoneQuery.PROJECTION,
98             FilteredNumbersUtil.PhoneQuery.SELECT_SEND_TO_VOICEMAIL_TRUE,
99             null,
100             null);
101     return cursorLoader;
102   }
103 
104   @Override
onLoadFinished(Loader<Cursor> loader, Cursor data)105   public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
106     adapter.swapCursor(data);
107   }
108 
109   @Override
onLoaderReset(Loader<Cursor> loader)110   public void onLoaderReset(Loader<Cursor> loader) {
111     adapter.swapCursor(null);
112   }
113 
114   @Override
onClick(final View view)115   public void onClick(final View view) {
116     if (view.getId() == R.id.import_button) {
117       FilteredNumbersUtil.importSendToVoicemailContacts(
118           getContext(),
119           new ImportSendToVoicemailContactsListener() {
120             @Override
121             public void onImportComplete() {
122               if (getActivity() != null) {
123                 getActivity().onBackPressed();
124               }
125             }
126           });
127     } else if (view.getId() == R.id.cancel_button) {
128       getActivity().onBackPressed();
129     }
130   }
131 }
132