Home | History | Annotate | Download | only in calldetails
      1 /*
      2  * Copyright (C) 2017 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.dialer.calldetails;
     18 
     19 import android.content.Context;
     20 import android.content.Intent;
     21 import android.os.AsyncTask;
     22 import android.os.Bundle;
     23 import android.provider.CallLog;
     24 import android.provider.CallLog.Calls;
     25 import android.support.annotation.NonNull;
     26 import android.support.v7.app.AppCompatActivity;
     27 import android.support.v7.widget.LinearLayoutManager;
     28 import android.support.v7.widget.RecyclerView;
     29 import android.support.v7.widget.Toolbar;
     30 import android.support.v7.widget.Toolbar.OnMenuItemClickListener;
     31 import android.view.MenuItem;
     32 import com.android.dialer.callcomposer.CallComposerContact;
     33 import com.android.dialer.calldetails.CallDetailsEntries.CallDetailsEntry;
     34 import com.android.dialer.common.Assert;
     35 import com.android.dialer.common.concurrent.AsyncTaskExecutors;
     36 import com.android.dialer.logging.DialerImpression;
     37 import com.android.dialer.logging.Logger;
     38 import com.android.dialer.protos.ProtoParsers;
     39 import java.util.List;
     40 
     41 /** Displays the details of a specific call log entry. */
     42 public class CallDetailsActivity extends AppCompatActivity implements OnMenuItemClickListener {
     43 
     44   private static final String EXTRA_CALL_DETAILS_ENTRIES = "call_details_entries";
     45   private static final String EXTRA_CONTACT = "contact";
     46   private static final String TASK_DELETE = "task_delete";
     47 
     48   private List<CallDetailsEntry> entries;
     49 
     50   public static Intent newInstance(
     51       Context context, @NonNull CallDetailsEntries details, @NonNull CallComposerContact contact) {
     52     Assert.isNotNull(details);
     53     Assert.isNotNull(contact);
     54 
     55     Intent intent = new Intent(context, CallDetailsActivity.class);
     56     ProtoParsers.put(intent, EXTRA_CONTACT, contact);
     57     ProtoParsers.put(intent, EXTRA_CALL_DETAILS_ENTRIES, details);
     58     return intent;
     59   }
     60 
     61   @Override
     62   protected void onCreate(Bundle savedInstanceState) {
     63     super.onCreate(savedInstanceState);
     64     setContentView(R.layout.call_details_activity);
     65     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
     66     toolbar.inflateMenu(R.menu.call_details_menu);
     67     toolbar.setOnMenuItemClickListener(this);
     68     toolbar.setTitle(R.string.call_details);
     69     onHandleIntent(getIntent());
     70   }
     71 
     72   @Override
     73   protected void onNewIntent(Intent intent) {
     74     super.onNewIntent(intent);
     75     onHandleIntent(intent);
     76   }
     77 
     78   private void onHandleIntent(Intent intent) {
     79     CallComposerContact contact =
     80         ProtoParsers.getTrusted(intent, EXTRA_CONTACT, CallComposerContact.getDefaultInstance());
     81     entries =
     82         ProtoParsers.getTrusted(
     83                 intent, EXTRA_CALL_DETAILS_ENTRIES, CallDetailsEntries.getDefaultInstance())
     84             .getEntriesList();
     85 
     86     RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
     87     recyclerView.setLayoutManager(new LinearLayoutManager(this));
     88     recyclerView.setAdapter(new CallDetailsAdapter(this, contact, entries));
     89   }
     90 
     91   @Override
     92   public boolean onMenuItemClick(MenuItem item) {
     93     if (item.getItemId() == R.id.call_detail_delete_menu_item) {
     94       Logger.get(this).logImpression(DialerImpression.Type.USER_DELETED_CALL_LOG_ITEM);
     95       AsyncTaskExecutors.createAsyncTaskExecutor().submit(TASK_DELETE, new DeleteCallsTask());
     96       item.setEnabled(false);
     97       return true;
     98     }
     99     return false;
    100   }
    101 
    102   /** Delete specified calls from the call log. */
    103   private class DeleteCallsTask extends AsyncTask<Void, Void, Void> {
    104 
    105     private final String callIds;
    106 
    107     DeleteCallsTask() {
    108       StringBuilder callIds = new StringBuilder();
    109       for (CallDetailsEntry entry : entries) {
    110         if (callIds.length() != 0) {
    111           callIds.append(",");
    112         }
    113         callIds.append(entry.getCallId());
    114       }
    115       this.callIds = callIds.toString();
    116     }
    117 
    118     @Override
    119     protected Void doInBackground(Void... params) {
    120       getContentResolver()
    121           .delete(Calls.CONTENT_URI, CallLog.Calls._ID + " IN (" + callIds + ")", null);
    122       return null;
    123     }
    124 
    125     @Override
    126     public void onPostExecute(Void result) {
    127       finish();
    128     }
    129   }
    130 }
    131