diff options
author | Raj Yengisetty <raj@cyngn.com> | 2016-01-28 15:30:07 -0800 |
---|---|---|
committer | Stephen Bird <sbird@cyngn.com> | 2016-04-08 10:17:16 -0700 |
commit | b0e43941352c88befa99a5e48f282d7adc3ab476 (patch) | |
tree | b7b037c3d616073356a9985ac9d0a9d73354a980 /src/com/android | |
parent | 8312e553af96fc46904ff4bccefcb22aea71d55d (diff) | |
download | android_packages_apps_Dialer-b0e43941352c88befa99a5e48f282d7adc3ab476.tar.gz android_packages_apps_Dialer-b0e43941352c88befa99a5e48f282d7adc3ab476.tar.bz2 android_packages_apps_Dialer-b0e43941352c88befa99a5e48f282d7adc3ab476.zip |
InCall: port nudges and fix plugin settings
Change-Id: I79f1c66e610419d18d7763b9063f775eacf85266
Diffstat (limited to 'src/com/android')
5 files changed, 303 insertions, 2 deletions
diff --git a/src/com/android/dialer/discovery/DiscoveryEventHandler.java b/src/com/android/dialer/discovery/DiscoveryEventHandler.java new file mode 100644 index 000000000..512538717 --- /dev/null +++ b/src/com/android/dialer/discovery/DiscoveryEventHandler.java @@ -0,0 +1,153 @@ +package com.android.dialer.discovery; + +import android.content.ComponentName; +import android.content.Context; +import android.content.SharedPreferences; +import android.content.pm.PackageManager; +import android.content.res.Resources; +import android.graphics.Bitmap; +import android.graphics.drawable.Drawable; +import android.os.Bundle; +import android.os.Parcelable; +import android.util.Log; + +import com.android.dialer.DialtactsActivity; +import com.android.phone.common.ambient.AmbientConnection; +import com.android.phone.common.incall.CallMethodUtils; +import com.android.phone.common.util.ImageUtils; + +import com.cyanogen.ambient.common.api.AmbientApiClient; +import com.cyanogen.ambient.discovery.DiscoveryManagerServices; +import com.cyanogen.ambient.discovery.NudgeServices; +import com.cyanogen.ambient.discovery.nudge.NotificationNudge; +import com.cyanogen.ambient.discovery.nudge.Nudge; +import com.cyanogen.ambient.discovery.util.NudgeKey; +import com.cyanogen.ambient.incall.InCallApi; +import com.cyanogen.ambient.incall.InCallServices; +import com.cyanogen.ambient.incall.results.InCallProviderInfoResult; +import com.cyanogen.ambient.incall.results.PluginStatusResult; +import com.cyanogen.ambient.plugin.PluginStatus; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class DiscoveryEventHandler { + + private static final String TAG = "DiscoveryEventHandler"; + private static final boolean DEBUG_STATUS = false; + + public static void getNudgeProvidersWithKey(final Context context, final String key) { + // Spin up a new thread to make the needed calls to ambient. + new Thread(new Runnable() { + @Override + public void run() { + AmbientApiClient client = AmbientConnection.CLIENT.get(context); + ArrayList<NotificationNudge> nudges = getNudges(client, context, key); + sendNudgeRequestToDiscovery(client, nudges); + } + }).start(); + } + + public static void sendNudgeRequestToDiscovery(AmbientApiClient client, + ArrayList<NotificationNudge> nudges) { + + for (NotificationNudge nn : nudges) { + DiscoveryManagerServices.DiscoveryManagerApi.publishNudge(client, nn); + } + } + + public static ArrayList<NotificationNudge> getNudges(AmbientApiClient client, Context context, + String key) { + + Map nudgePlugins = + NudgeServices.NudgeApi.getAvailableNudgesForKey(client, key).await().components; + + InCallApi api = InCallServices.getInstance(); + + List<ComponentName> plugins = api.getInstalledPlugins(client).await().components; + + HashMap<String, Bundle> availableNudges = new HashMap<>(); + + for (Object entry : nudgePlugins.entrySet()) { + Map.Entry<ComponentName, Bundle> theEntry = (Map.Entry<ComponentName, Bundle>) entry; + availableNudges.put(theEntry.getKey().getPackageName(), theEntry.getValue()); + } + ArrayList<NotificationNudge> notificationNudges = new ArrayList<>(); + + if (plugins != null && plugins.size() > 0) { + + for (ComponentName component : plugins) { + + if (availableNudges.containsKey(component.getPackageName())) { + + PluginStatusResult statusResult = + api.getPluginStatus(client, component).await(); + + Bundle b = availableNudges.get(component.getPackageName()); + + if (key.equals(NudgeKey.NOTIFICATION_INTERNATIONAL_CALL)) { + SharedPreferences preferences = context + .getSharedPreferences(DialtactsActivity.SHARED_PREFS_NAME, + Context.MODE_PRIVATE); + int count = preferences.getInt(CallMethodUtils.PREF_INTERNATIONAL_CALLS, 0); + boolean checkCount = + count != b.getInt(NudgeKey.NOTIFICATION_PARAM_EVENTS_FIRST_NUDGE) || + count != b.getInt(NudgeKey.NOTIFICATION_PARAM_EVENTS_SECOND_NUDGE); + + if (checkCount) { + // Nudge not yet ready for this item. + continue; + } + } + + if (DEBUG_STATUS || (statusResult.status != PluginStatus.DISABLED && + statusResult.status != PluginStatus.UNAVAILABLE)) { + + Bitmap notificationIcon = null; + + InCallProviderInfoResult providerInfo = + api.getProviderInfo(client, component).await(); + + if (providerInfo != null && providerInfo.inCallProviderInfo != null) { + try { + Resources pluginResources = context.getPackageManager() + .getResourcesForApplication(component.getPackageName()); + + Drawable d = pluginResources.getDrawable( + providerInfo.inCallProviderInfo.getBrandIcon(), null); + + notificationIcon = ImageUtils.drawableToBitmap(d); + } catch (Resources.NotFoundException e) { + Log.e(TAG, "Unable to retrieve icon for plugin: " + component); + } catch (PackageManager.NameNotFoundException e) { + Log.e(TAG, "Plugin isn't installed: " + component); + } + } + + NotificationNudge nn = new NotificationNudge(component.getPackageName(), + Nudge.Type.IMMEDIATE, + b.getString(NudgeKey.NUDGE_PARAM_TITLE), + b.getString(NudgeKey.NOTIFICATION_PARAM_BODY)); + + if (notificationIcon != null) { + nn.setLargeIcon(notificationIcon); + } + + Parcelable[] actions = + b.getParcelableArray(NudgeKey.NOTIFICATION_PARAM_NUDGE_ACTIONS); + + for (Parcelable action : actions) { + NotificationNudge.Button button = (NotificationNudge.Button) action; + nn.addButton(button); + } + + notificationNudges.add(nn); + } + } + } + } + return notificationNudges; + } +} diff --git a/src/com/android/dialer/discovery/DiscoveryService.java b/src/com/android/dialer/discovery/DiscoveryService.java new file mode 100644 index 000000000..e4767e7bc --- /dev/null +++ b/src/com/android/dialer/discovery/DiscoveryService.java @@ -0,0 +1,33 @@ +package com.android.dialer.discovery; + +import android.app.IntentService; +import android.content.Intent; +import android.net.ConnectivityManager; + +import com.android.dialer.discovery.DiscoveryEventHandler; +import com.cyanogen.ambient.discovery.util.NudgeKey; +import com.cyanogen.ambient.incall.InCallServices; + +public class DiscoveryService extends IntentService { + + private static final String TAG = DiscoveryService.class.getSimpleName(); + + public DiscoveryService() { + super(TAG); + } + + @Override + protected void onHandleIntent(Intent intent) { + String action = intent.getAction(); + switch (action) { + case ConnectivityManager.CONNECTIVITY_ACTION: + new DiscoveryEventHandler().getNudgeProvidersWithKey(getApplicationContext(), + NudgeKey.NOTIFICATION_ROAMING); + break; + case Intent.ACTION_NEW_OUTGOING_CALL: + new DiscoveryEventHandler().getNudgeProvidersWithKey(getApplicationContext(), + NudgeKey.NOTIFICATION_INTERNATIONAL_CALL); + break; + } + } +} diff --git a/src/com/android/dialer/discovery/DiscoverySignalReceiver.java b/src/com/android/dialer/discovery/DiscoverySignalReceiver.java new file mode 100644 index 000000000..5ba7ceecc --- /dev/null +++ b/src/com/android/dialer/discovery/DiscoverySignalReceiver.java @@ -0,0 +1,109 @@ +package com.android.dialer.discovery; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; +import android.telephony.TelephonyManager; + +import android.util.Log; +import com.android.contacts.common.GeoUtil; +import com.android.dialer.DialtactsActivity; +import com.android.dialer.discovery.DiscoveryService; +import com.android.phone.common.incall.CallMethodUtils; +import com.google.i18n.phonenumbers.NumberParseException; +import com.google.i18n.phonenumbers.PhoneNumberUtil; +import com.google.i18n.phonenumbers.Phonemetadata; +import com.google.i18n.phonenumbers.Phonenumber; + +public class DiscoverySignalReceiver extends BroadcastReceiver { + + private static final String TAG = "DiscoverySignalReceiver"; + private static final boolean DEBUG_CONNECTIVITY = false; + + @Override + public void onReceive(Context context, Intent intent) { + + String action = intent.getAction(); + + switch (action) { + case ConnectivityManager.CONNECTIVITY_ACTION: + ConnectivityManager connManager = (ConnectivityManager) + context.getSystemService(Context.CONNECTIVITY_SERVICE); + + NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); + + TelephonyManager tm = + (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); + + if ((mWifi.isConnected() && tm.isNetworkRoaming()) || DEBUG_CONNECTIVITY) { + startServiceForConnectivityChanged(context); + } + break; + case Intent.ACTION_NEW_OUTGOING_CALL: + String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); + SharedPreferences preferences = context + .getSharedPreferences(DialtactsActivity.SHARED_PREFS_NAME, + Context.MODE_PRIVATE); + int currentCount = preferences.getInt(CallMethodUtils.PREF_INTERNATIONAL_CALLS, 0); + if (isMaybeInternationalNumber(context, phoneNumber)) { + preferences.edit().putInt(CallMethodUtils.PREF_INTERNATIONAL_CALLS, + ++currentCount).apply(); + startServiceForInternationalCallMade(context); + } + break; + } + + } + + public boolean isMaybeInternationalNumber(Context context, String number) { + PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); + String currentCountryIso = GeoUtil.getCurrentCountryIso(context); + + try { + Phonenumber.PhoneNumber phoneNumberCalling = phoneUtil.parse(number, currentCountryIso); + + int callingToCode = phoneNumberCalling.getCountryCode(); + int callingFromCode = phoneUtil.getCountryCodeForRegion(currentCountryIso); + + if (callingToCode == 1) { + if (phoneUtil.isNANPACountry(currentCountryIso)) { + // This is NANP country + // There is no way to reliably know what country we are calling to + // this even catches local numbers, we must return false to avoid the + // repercussions of false positives. The only way to know for sure is to build + // an area code list and keep that up to date. + return false; + } + } + + // if true, we are making an international call + // if false, we are making a local call or a call between countries that share calling + // codes + return callingToCode != callingFromCode; + + } catch (NumberParseException e) { + Log.e(TAG, "Phone number is invalid", e); + } + return false; + } + + private void startServiceForInternationalCallMade(Context context) { + Intent serviceIntent = getIntentForDiscoveryService(context); + serviceIntent.setAction(Intent.ACTION_NEW_OUTGOING_CALL); + context.startService(serviceIntent); + } + + private void startServiceForConnectivityChanged(Context context) { + Intent serviceIntent = getIntentForDiscoveryService(context); + serviceIntent.setAction(ConnectivityManager.CONNECTIVITY_ACTION); + context.startService(serviceIntent); + } + + private Intent getIntentForDiscoveryService(Context context) { + return new Intent(context, DiscoveryService.class); + } + +} diff --git a/src/com/android/dialer/incall/CallMethodStatusReceiver.java b/src/com/android/dialer/incall/CallMethodStatusReceiver.java index a5af807fe..b0d67b1ec 100644 --- a/src/com/android/dialer/incall/CallMethodStatusReceiver.java +++ b/src/com/android/dialer/incall/CallMethodStatusReceiver.java @@ -5,7 +5,9 @@ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; + import com.android.dialer.DialtactsActivity; +import com.android.phone.common.incall.CallMethodHelper; import com.android.phone.common.incall.CallMethodUtils; import com.cyanogen.ambient.plugin.PluginStatus; import com.cyanogen.ambient.plugin.PluginStatusConstants; @@ -16,6 +18,8 @@ public class CallMethodStatusReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { Bundle b = intent.getExtras(); + CallMethodHelper.refresh(); + SharedPreferences preferences = context .getSharedPreferences(DialtactsActivity.SHARED_PREFS_NAME, Context.MODE_PRIVATE); diff --git a/src/com/android/dialer/settings/DialerSettingsActivity.java b/src/com/android/dialer/settings/DialerSettingsActivity.java index 7d1adc202..f5d3b9dc8 100644 --- a/src/com/android/dialer/settings/DialerSettingsActivity.java +++ b/src/com/android/dialer/settings/DialerSettingsActivity.java @@ -431,7 +431,9 @@ public class DialerSettingsActivity extends PreferenceActivity { // by the ambient core package } }, componentName); - asyncTask.execute(isEnabled ? PluginStatus.ENABLED : PluginStatus.DISABLED); - extras.putBoolean(COMPONENT_STATUS, isEnabled); + + int status = isEnabled ? PluginStatus.ENABLED : PluginStatus.DISABLED; + asyncTask.execute(status); + extras.putInt(COMPONENT_STATUS, status); } } |