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