• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.voicemail.impl;
18 
19 import android.annotation.TargetApi;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.SharedPreferences;
24 import android.database.Cursor;
25 import android.net.Uri;
26 import android.preference.PreferenceManager;
27 import android.provider.CallLog.Calls;
28 import android.provider.VoicemailContract.Voicemails;
29 import android.telecom.PhoneAccountHandle;
30 import android.telecom.TelecomManager;
31 import com.android.dialer.common.LogUtil;
32 import com.android.dialer.common.concurrent.DialerExecutor.Worker;
33 import com.android.dialer.common.concurrent.DialerExecutorComponent;
34 import com.android.voicemail.VoicemailComponent;
35 import com.android.voicemail.VoicemailVersionConstants;
36 
37 /**
38  * Receives MY_PACKAGE_REPLACED to trigger VVM activation and to check for legacy voicemail users.
39  */
40 public class PackageReplacedReceiver extends BroadcastReceiver {
41 
42   @Override
onReceive(Context context, Intent intent)43   public void onReceive(Context context, Intent intent) {
44     VvmLog.i("PackageReplacedReceiver.onReceive", "package replaced, starting activation");
45 
46     if (!VoicemailComponent.get(context).getVoicemailClient().isVoicemailModuleEnabled()) {
47       VvmLog.e("PackageReplacedReceiver.onReceive", "module disabled");
48       return;
49     }
50 
51     for (PhoneAccountHandle phoneAccountHandle :
52         context.getSystemService(TelecomManager.class).getCallCapablePhoneAccounts()) {
53       ActivationTask.start(context, phoneAccountHandle, null);
54     }
55 
56     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
57     if (!prefs.contains(VoicemailVersionConstants.PREF_DIALER_FEATURE_VERSION_ACKNOWLEDGED_KEY)) {
58       setVoicemailFeatureVersionAsync(context);
59     }
60   }
61 
setVoicemailFeatureVersionAsync(Context context)62   private void setVoicemailFeatureVersionAsync(Context context) {
63     LogUtil.enterBlock("PackageReplacedReceiver.setVoicemailFeatureVersionAsync");
64 
65     // Check if user is already using voicemail (ie do they have any voicemails), and set the
66     // acknowledged feature value accordingly.
67     PendingResult pendingResult = goAsync();
68     DialerExecutorComponent.get(context)
69         .dialerExecutorFactory()
70         .createNonUiTaskBuilder(new ExistingVoicemailCheck(context))
71         .onSuccess(
72             output -> {
73               LogUtil.i("PackageReplacedReceiver.setVoicemailFeatureVersionAsync", "success");
74               pendingResult.finish();
75             })
76         .onFailure(
77             throwable -> {
78               LogUtil.i("PackageReplacedReceiver.setVoicemailFeatureVersionAsync", "failure");
79               pendingResult.finish();
80             })
81         .build()
82         .executeParallel(null);
83   }
84 
85   private static class ExistingVoicemailCheck implements Worker<Void, Void> {
86     private static final String[] PROJECTION = new String[] {Voicemails._ID};
87 
88     private final Context context;
89 
ExistingVoicemailCheck(Context context)90     ExistingVoicemailCheck(Context context) {
91       this.context = context;
92     }
93 
94     @TargetApi(android.os.Build.VERSION_CODES.M) // used for try with resources
95     @Override
doInBackground(Void arg)96     public Void doInBackground(Void arg) throws Throwable {
97       LogUtil.i("PackageReplacedReceiver.ExistingVoicemailCheck.doInBackground", "");
98 
99       // Check the database for existing voicemails.
100       boolean hasVoicemails = false;
101       Uri uri = Voicemails.buildSourceUri(context.getPackageName());
102       String whereClause = Calls.TYPE + " = " + Calls.VOICEMAIL_TYPE;
103       try (Cursor cursor =
104           context.getContentResolver().query(uri, PROJECTION, whereClause, null, null)) {
105         if (cursor == null) {
106           LogUtil.e(
107               "PackageReplacedReceiver.ExistingVoicemailCheck.doInBackground",
108               "failed to check for existing voicemails");
109         } else if (cursor.moveToFirst()) {
110           hasVoicemails = true;
111         }
112       }
113 
114       LogUtil.i(
115           "PackageReplacedReceiver.ExistingVoicemailCheck.doInBackground",
116           "has voicemails: " + hasVoicemails);
117       int version = hasVoicemails ? VoicemailVersionConstants.LEGACY_VOICEMAIL_FEATURE_VERSION : 0;
118       PreferenceManager.getDefaultSharedPreferences(context)
119           .edit()
120           .putInt(VoicemailVersionConstants.PREF_DIALER_FEATURE_VERSION_ACKNOWLEDGED_KEY, version)
121           .apply();
122       return null;
123     }
124   }
125 }
126