Wenyi Wang | 1fc3ef4 | 2016-01-14 18:21:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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.contacts.callblocking; |
| 18 | |
| 19 | import android.app.ListFragment; |
| 20 | import android.app.LoaderManager; |
| 21 | import android.content.Context; |
| 22 | import android.content.CursorLoader; |
| 23 | import android.content.Loader; |
| 24 | import android.database.Cursor; |
Wenyi Wang | 1fc3ef4 | 2016-01-14 18:21:40 -0800 | [diff] [blame] | 25 | import android.os.Bundle; |
| 26 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
Wenyi Wang | 1fc3ef4 | 2016-01-14 18:21:40 -0800 | [diff] [blame] | 27 | import android.support.v7.app.ActionBar; |
| 28 | import android.support.v7.app.AppCompatActivity; |
Wenyi Wang | 1fc3ef4 | 2016-01-14 18:21:40 -0800 | [diff] [blame] | 29 | import android.view.LayoutInflater; |
| 30 | import android.view.View; |
| 31 | import android.view.ViewGroup; |
| 32 | |
| 33 | import com.android.contacts.R; |
Wenyi Wang | 1fc3ef4 | 2016-01-14 18:21:40 -0800 | [diff] [blame] | 34 | import com.android.contacts.callblocking.FilteredNumbersUtil.ImportSendToVoicemailContactsListener; |
| 35 | |
Wenyi Wang | 1fc3ef4 | 2016-01-14 18:21:40 -0800 | [diff] [blame] | 36 | public class ViewNumbersToImportFragment extends ListFragment |
| 37 | implements LoaderManager.LoaderCallbacks<Cursor>, |
| 38 | View.OnClickListener { |
| 39 | |
| 40 | private ViewNumbersToImportAdapter mAdapter; |
| 41 | |
| 42 | @Override |
| 43 | public Context getContext() { |
| 44 | return getActivity(); |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public void onActivityCreated(Bundle savedInstanceState) { |
| 49 | super.onActivityCreated(savedInstanceState); |
| 50 | |
| 51 | if (mAdapter == null) { |
| 52 | mAdapter = ViewNumbersToImportAdapter.newViewNumbersToImportAdapter( |
| 53 | getContext(), getActivity().getFragmentManager()); |
| 54 | } |
| 55 | setListAdapter(mAdapter); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public void onDestroy() { |
| 60 | setListAdapter(null); |
| 61 | super.onDestroy(); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public void onCreate(Bundle savedInstanceState) { |
| 66 | super.onCreate(savedInstanceState); |
| 67 | getLoaderManager().initLoader(0, null, this); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 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 |
| 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 |
| 92 | public Loader<Cursor> onCreateLoader(int id, Bundle args) { |
| 93 | final CursorLoader cursorLoader = 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 | switch (view.getId()) { |
| 116 | case R.id.import_button: |
| 117 | FilteredNumbersUtil.importSendToVoicemailContacts(getContext(), |
| 118 | new ImportSendToVoicemailContactsListener() { |
| 119 | @Override |
| 120 | public void onImportComplete() { |
| 121 | if (getActivity() != null) { |
| 122 | getActivity().onBackPressed(); |
| 123 | } |
| 124 | } |
| 125 | }); |
| 126 | break; |
| 127 | case R.id.cancel_button: |
| 128 | getActivity().onBackPressed(); |
| 129 | break; |
| 130 | } |
| 131 | } |
| 132 | } |