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.server.telecom.settings; 18 19 import android.annotation.Nullable; 20 import android.app.Fragment; 21 import android.content.ContentResolver; 22 import android.content.ContentValues; 23 import android.os.AsyncTask; 24 import android.os.Bundle; 25 import android.provider.BlockedNumberContract; 26 27 /** 28 * Retained fragment that runs an async task to add a blocked number. 29 * 30 * <p>We run the task inside a retained fragment so that if the screen orientation changed, the 31 * task does not get lost. 32 */ 33 public class BlockNumberTaskFragment extends Fragment { 34 @Nullable private BlockNumberTask mTask; 35 @Nullable Listener mListener; 36 37 /** 38 * Task to block a number. 39 */ 40 private class BlockNumberTask extends AsyncTask<String, Void, Boolean> { 41 private String mNumber; 42 43 /** 44 * @return true if number was blocked; false if number is already blocked. 45 */ 46 @Override doInBackground(String... params)47 protected Boolean doInBackground(String... params) { 48 mNumber = params[0]; 49 if (BlockedNumberContract.isBlocked(getContext(), mNumber)) { 50 return false; 51 } else { 52 ContentResolver contentResolver = getContext().getContentResolver(); 53 ContentValues newValues = new ContentValues(); 54 newValues.put(BlockedNumberContract.BlockedNumbers.COLUMN_ORIGINAL_NUMBER, 55 mNumber); 56 contentResolver.insert(BlockedNumberContract.BlockedNumbers.CONTENT_URI, 57 newValues); 58 return true; 59 } 60 } 61 62 @Override onPostExecute(Boolean result)63 protected void onPostExecute(Boolean result) { 64 mTask = null; 65 if (mListener != null) { 66 mListener.onBlocked(mNumber, !result /* alreadyBlocked */); 67 } 68 mListener = null; 69 } 70 } 71 72 public interface Listener { onBlocked(String number, boolean alreadyBlocked)73 void onBlocked(String number, boolean alreadyBlocked); 74 } 75 76 @Override onCreate(Bundle savedInstanceState)77 public void onCreate(Bundle savedInstanceState) { 78 super.onCreate(savedInstanceState); 79 setRetainInstance(true); 80 } 81 82 @Override onDestroy()83 public void onDestroy() { 84 if (mTask != null) { 85 mTask.cancel(true /* mayInterruptIfRunning */); 86 } 87 super.onDestroy(); 88 } 89 90 /** 91 * Runs an async task to write the number to the blocked numbers provider if it does not already 92 * exist. 93 * 94 * Triggers {@link Listener#onBlocked(String, boolean)} when task finishes to show proper UI. 95 */ blockIfNotAlreadyBlocked(String number, Listener listener)96 public void blockIfNotAlreadyBlocked(String number, Listener listener) { 97 mListener = listener; 98 mTask = new BlockNumberTask(); 99 mTask.execute(number); 100 } 101 }