149e260af3269053b18a9e98b15e172e9ecee2d9eMohamed/* 249e260af3269053b18a9e98b15e172e9ecee2d9eMohamed * Copyright (C) 2016 The Android Open Source Project 349e260af3269053b18a9e98b15e172e9ecee2d9eMohamed * 449e260af3269053b18a9e98b15e172e9ecee2d9eMohamed * Licensed under the Apache License, Version 2.0 (the "License"); 549e260af3269053b18a9e98b15e172e9ecee2d9eMohamed * you may not use this file except in compliance with the License. 649e260af3269053b18a9e98b15e172e9ecee2d9eMohamed * You may obtain a copy of the License at 749e260af3269053b18a9e98b15e172e9ecee2d9eMohamed * 849e260af3269053b18a9e98b15e172e9ecee2d9eMohamed * http://www.apache.org/licenses/LICENSE-2.0 949e260af3269053b18a9e98b15e172e9ecee2d9eMohamed * 1049e260af3269053b18a9e98b15e172e9ecee2d9eMohamed * Unless required by applicable law or agreed to in writing, software 1149e260af3269053b18a9e98b15e172e9ecee2d9eMohamed * distributed under the License is distributed on an "AS IS" BASIS, 1249e260af3269053b18a9e98b15e172e9ecee2d9eMohamed * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1349e260af3269053b18a9e98b15e172e9ecee2d9eMohamed * See the License for the specific language governing permissions and 1449e260af3269053b18a9e98b15e172e9ecee2d9eMohamed * limitations under the License 1549e260af3269053b18a9e98b15e172e9ecee2d9eMohamed */ 1649e260af3269053b18a9e98b15e172e9ecee2d9eMohamed 1749e260af3269053b18a9e98b15e172e9ecee2d9eMohamedpackage com.android.server.telecom.settings; 1849e260af3269053b18a9e98b15e172e9ecee2d9eMohamed 1949e260af3269053b18a9e98b15e172e9ecee2d9eMohamedimport android.annotation.Nullable; 2049e260af3269053b18a9e98b15e172e9ecee2d9eMohamedimport android.app.Fragment; 2149e260af3269053b18a9e98b15e172e9ecee2d9eMohamedimport android.content.ContentResolver; 2249e260af3269053b18a9e98b15e172e9ecee2d9eMohamedimport android.content.ContentValues; 2349e260af3269053b18a9e98b15e172e9ecee2d9eMohamedimport android.os.AsyncTask; 2449e260af3269053b18a9e98b15e172e9ecee2d9eMohamedimport android.os.Bundle; 2549e260af3269053b18a9e98b15e172e9ecee2d9eMohamedimport android.provider.BlockedNumberContract; 2649e260af3269053b18a9e98b15e172e9ecee2d9eMohamedimport com.android.server.telecom.R; 2749e260af3269053b18a9e98b15e172e9ecee2d9eMohamed 2849e260af3269053b18a9e98b15e172e9ecee2d9eMohamed/** 2949e260af3269053b18a9e98b15e172e9ecee2d9eMohamed * Retained fragment that runs an async task to add a blocked number. 3049e260af3269053b18a9e98b15e172e9ecee2d9eMohamed * 3149e260af3269053b18a9e98b15e172e9ecee2d9eMohamed * <p>We run the task inside a retained fragment so that if the screen orientation changed, the 3249e260af3269053b18a9e98b15e172e9ecee2d9eMohamed * task does not get lost. 3349e260af3269053b18a9e98b15e172e9ecee2d9eMohamed */ 3449e260af3269053b18a9e98b15e172e9ecee2d9eMohamedpublic class BlockNumberTaskFragment extends Fragment { 3549e260af3269053b18a9e98b15e172e9ecee2d9eMohamed @Nullable private BlockNumberTask mTask; 3649e260af3269053b18a9e98b15e172e9ecee2d9eMohamed @Nullable Listener mListener; 3749e260af3269053b18a9e98b15e172e9ecee2d9eMohamed 3849e260af3269053b18a9e98b15e172e9ecee2d9eMohamed /** 3949e260af3269053b18a9e98b15e172e9ecee2d9eMohamed * Task to block a number. 4049e260af3269053b18a9e98b15e172e9ecee2d9eMohamed */ 4149e260af3269053b18a9e98b15e172e9ecee2d9eMohamed private class BlockNumberTask extends AsyncTask<String, Void, Boolean> { 4249e260af3269053b18a9e98b15e172e9ecee2d9eMohamed private String mNumber; 4349e260af3269053b18a9e98b15e172e9ecee2d9eMohamed 4449e260af3269053b18a9e98b15e172e9ecee2d9eMohamed /** 4549e260af3269053b18a9e98b15e172e9ecee2d9eMohamed * @return true if number was blocked; false if number is already blocked. 4649e260af3269053b18a9e98b15e172e9ecee2d9eMohamed */ 4749e260af3269053b18a9e98b15e172e9ecee2d9eMohamed @Override 4849e260af3269053b18a9e98b15e172e9ecee2d9eMohamed protected Boolean doInBackground(String... params) { 4949e260af3269053b18a9e98b15e172e9ecee2d9eMohamed mNumber = params[0]; 5049e260af3269053b18a9e98b15e172e9ecee2d9eMohamed if (BlockedNumberContract.isBlocked(getContext(), mNumber)) { 5149e260af3269053b18a9e98b15e172e9ecee2d9eMohamed return false; 5249e260af3269053b18a9e98b15e172e9ecee2d9eMohamed } else { 5349e260af3269053b18a9e98b15e172e9ecee2d9eMohamed ContentResolver contentResolver = getContext().getContentResolver(); 5449e260af3269053b18a9e98b15e172e9ecee2d9eMohamed ContentValues newValues = new ContentValues(); 5549e260af3269053b18a9e98b15e172e9ecee2d9eMohamed newValues.put(BlockedNumberContract.BlockedNumbers.COLUMN_ORIGINAL_NUMBER, 5649e260af3269053b18a9e98b15e172e9ecee2d9eMohamed mNumber); 5749e260af3269053b18a9e98b15e172e9ecee2d9eMohamed contentResolver.insert(BlockedNumberContract.BlockedNumbers.CONTENT_URI, 5849e260af3269053b18a9e98b15e172e9ecee2d9eMohamed newValues); 5949e260af3269053b18a9e98b15e172e9ecee2d9eMohamed return true; 6049e260af3269053b18a9e98b15e172e9ecee2d9eMohamed } 6149e260af3269053b18a9e98b15e172e9ecee2d9eMohamed } 6249e260af3269053b18a9e98b15e172e9ecee2d9eMohamed 6349e260af3269053b18a9e98b15e172e9ecee2d9eMohamed @Override 6449e260af3269053b18a9e98b15e172e9ecee2d9eMohamed protected void onPostExecute(Boolean result) { 6549e260af3269053b18a9e98b15e172e9ecee2d9eMohamed mTask = null; 6649e260af3269053b18a9e98b15e172e9ecee2d9eMohamed if (mListener != null) { 6749e260af3269053b18a9e98b15e172e9ecee2d9eMohamed mListener.onBlocked(mNumber, !result /* alreadyBlocked */); 6849e260af3269053b18a9e98b15e172e9ecee2d9eMohamed } 6949e260af3269053b18a9e98b15e172e9ecee2d9eMohamed mListener = null; 7049e260af3269053b18a9e98b15e172e9ecee2d9eMohamed } 7149e260af3269053b18a9e98b15e172e9ecee2d9eMohamed } 7249e260af3269053b18a9e98b15e172e9ecee2d9eMohamed 7349e260af3269053b18a9e98b15e172e9ecee2d9eMohamed public interface Listener { 7449e260af3269053b18a9e98b15e172e9ecee2d9eMohamed void onBlocked(String number, boolean alreadyBlocked); 7549e260af3269053b18a9e98b15e172e9ecee2d9eMohamed } 7649e260af3269053b18a9e98b15e172e9ecee2d9eMohamed 7749e260af3269053b18a9e98b15e172e9ecee2d9eMohamed @Override 7849e260af3269053b18a9e98b15e172e9ecee2d9eMohamed public void onCreate(Bundle savedInstanceState) { 7949e260af3269053b18a9e98b15e172e9ecee2d9eMohamed super.onCreate(savedInstanceState); 8049e260af3269053b18a9e98b15e172e9ecee2d9eMohamed setRetainInstance(true); 8149e260af3269053b18a9e98b15e172e9ecee2d9eMohamed } 8249e260af3269053b18a9e98b15e172e9ecee2d9eMohamed 8349e260af3269053b18a9e98b15e172e9ecee2d9eMohamed @Override 8449e260af3269053b18a9e98b15e172e9ecee2d9eMohamed public void onDestroy() { 8549e260af3269053b18a9e98b15e172e9ecee2d9eMohamed if (mTask != null) { 8649e260af3269053b18a9e98b15e172e9ecee2d9eMohamed mTask.cancel(true /* mayInterruptIfRunning */); 8749e260af3269053b18a9e98b15e172e9ecee2d9eMohamed } 8849e260af3269053b18a9e98b15e172e9ecee2d9eMohamed super.onDestroy(); 8949e260af3269053b18a9e98b15e172e9ecee2d9eMohamed } 9049e260af3269053b18a9e98b15e172e9ecee2d9eMohamed 9149e260af3269053b18a9e98b15e172e9ecee2d9eMohamed /** 9249e260af3269053b18a9e98b15e172e9ecee2d9eMohamed * Runs an async task to write the number to the blocked numbers provider if it does not already 9349e260af3269053b18a9e98b15e172e9ecee2d9eMohamed * exist. 9449e260af3269053b18a9e98b15e172e9ecee2d9eMohamed * 9549e260af3269053b18a9e98b15e172e9ecee2d9eMohamed * Triggers {@link Listener#onBlocked(String, boolean)} when task finishes to show proper UI. 9649e260af3269053b18a9e98b15e172e9ecee2d9eMohamed */ 9749e260af3269053b18a9e98b15e172e9ecee2d9eMohamed public void blockIfNotAlreadyBlocked(String number, Listener listener) { 9849e260af3269053b18a9e98b15e172e9ecee2d9eMohamed mListener = listener; 9949e260af3269053b18a9e98b15e172e9ecee2d9eMohamed mTask = new BlockNumberTask(); 10049e260af3269053b18a9e98b15e172e9ecee2d9eMohamed mTask.execute(number); 10149e260af3269053b18a9e98b15e172e9ecee2d9eMohamed } 10249e260af3269053b18a9e98b15e172e9ecee2d9eMohamed}