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