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.simulator.impl; 18 19 import android.content.Context; 20 import android.content.Intent; 21 import android.os.AsyncTask; 22 import android.provider.VoicemailContract; 23 import android.support.annotation.NonNull; 24 import android.support.annotation.Nullable; 25 import android.view.ActionProvider; 26 import android.view.MenuItem; 27 import android.view.SubMenu; 28 import android.view.View; 29 import com.android.dialer.common.Assert; 30 import com.android.dialer.common.LogUtil; 31 import com.android.dialer.common.concurrent.DialerExecutor.Worker; 32 import com.android.dialer.common.concurrent.DialerExecutors; 33 import com.android.dialer.enrichedcall.simulator.EnrichedCallSimulatorActivity; 34 import com.android.dialer.persistentlog.PersistentLogger; 35 36 /** Implements the simulator submenu. */ 37 final class SimulatorActionProvider extends ActionProvider { 38 @NonNull private final Context context; 39 40 private static class ShareLogWorker implements Worker<Void, String> { 41 42 @Nullable 43 @Override 44 public String doInBackground(Void unused) { 45 return PersistentLogger.dumpLogToString(); 46 } 47 } 48 49 public SimulatorActionProvider(@NonNull Context context) { 50 super(Assert.isNotNull(context)); 51 this.context = context; 52 } 53 54 @Override 55 public View onCreateActionView() { 56 LogUtil.enterBlock("SimulatorActionProvider.onCreateActionView(null)"); 57 return null; 58 } 59 60 @Override 61 public View onCreateActionView(MenuItem forItem) { 62 LogUtil.enterBlock("SimulatorActionProvider.onCreateActionView(MenuItem)"); 63 return null; 64 } 65 66 @Override 67 public boolean hasSubMenu() { 68 LogUtil.enterBlock("SimulatorActionProvider.hasSubMenu"); 69 return true; 70 } 71 72 @Override 73 public void onPrepareSubMenu(SubMenu subMenu) { 74 super.onPrepareSubMenu(subMenu); 75 LogUtil.enterBlock("SimulatorActionProvider.onPrepareSubMenu"); 76 subMenu.clear(); 77 subMenu 78 .add("Add call") 79 .setOnMenuItemClickListener( 80 (item) -> { 81 SimulatorVoiceCall.addNewIncomingCall(context); 82 return true; 83 }); 84 subMenu 85 .add("Populate database") 86 .setOnMenuItemClickListener( 87 (item) -> { 88 populateDatabase(); 89 return true; 90 }); 91 subMenu 92 .add("Sync Voicemail") 93 .setOnMenuItemClickListener( 94 (item) -> { 95 Intent intent = new Intent(VoicemailContract.ACTION_SYNC_VOICEMAIL); 96 context.sendBroadcast(intent); 97 return true; 98 }); 99 100 subMenu 101 .add("Share persistent log") 102 .setOnMenuItemClickListener( 103 (item) -> { 104 DialerExecutors.createNonUiTaskBuilder(new ShareLogWorker()) 105 .onSuccess( 106 (String log) -> { 107 Intent intent = new Intent(Intent.ACTION_SEND); 108 intent.setType("text/plain"); 109 intent.putExtra(Intent.EXTRA_TEXT, log); 110 if (intent.resolveActivity(context.getPackageManager()) != null) { 111 context.startActivity(intent); 112 } 113 }) 114 .build() 115 .executeSerial(null); 116 return true; 117 }); 118 subMenu 119 .add("Enriched call simulator") 120 .setOnMenuItemClickListener( 121 (item) -> { 122 context.startActivity(EnrichedCallSimulatorActivity.newIntent(context)); 123 return true; 124 }); 125 } 126 127 private void populateDatabase() { 128 new AsyncTask<Void, Void, Void>() { 129 @Override 130 public Void doInBackground(Void... params) { 131 SimulatorContacts.populateContacts(context); 132 SimulatorCallLog.populateCallLog(context); 133 SimulatorVoicemail.populateVoicemail(context); 134 return null; 135 } 136 }.execute(); 137 } 138 } 139