diff options
| author | Artem Shvadskiy <ashvadskiy@cyngn.com> | 2016-05-09 15:59:22 -0700 |
|---|---|---|
| committer | Rohit Yengisetty <rohit@cyngn.com> | 2016-05-09 18:20:16 -0700 |
| commit | 214a3faf44a42a6aed9271d21a90dea88678bfd6 (patch) | |
| tree | 1fa3aef6158917a60072361b69bddabfecc9464a /src/com | |
| parent | 50c3a511aabc3f24161f9e61c6b06c0c3a8b6d12 (diff) | |
| download | android_packages_apps_Dialer-214a3faf44a42a6aed9271d21a90dea88678bfd6.tar.gz android_packages_apps_Dialer-214a3faf44a42a6aed9271d21a90dea88678bfd6.tar.bz2 android_packages_apps_Dialer-214a3faf44a42a6aed9271d21a90dea88678bfd6.zip | |
Add Caller-info provider nudges and adapt new CallerInfoApi
CallerInfoApi within AmbientSDK has undergone a revision. There
are changes to authentication APIs for Plugins and also to the
LookupProvider APIs.
Change-Id: Ia247925c7797e290ec05214bdca27ec70e41abda
issue-id: DIALER-719 DIALER-724
Diffstat (limited to 'src/com')
13 files changed, 337 insertions, 165 deletions
diff --git a/src/com/android/dialer/CallDetailActivity.java b/src/com/android/dialer/CallDetailActivity.java index 0509fca6f..b5b24150a 100644 --- a/src/com/android/dialer/CallDetailActivity.java +++ b/src/com/android/dialer/CallDetailActivity.java @@ -63,6 +63,7 @@ import com.android.dialer.widget.DialerQuickContact; import com.android.phone.common.incall.CallMethodInfo; import com.cyanogen.ambient.incall.extension.OriginCodes; +import com.cyanogen.lookup.phonenumber.contract.LookupProvider; import com.cyanogen.lookup.phonenumber.provider.LookupProviderImpl; /** @@ -222,6 +223,7 @@ public class CallDetailActivity extends Activity private TextView mCallerNumber; private TextView mAccountLabel; private View mCallButton; + private LookupProvider mLookupProvider; private ContactInfoHelper mContactInfoHelper; private BlockContactHelper mBlockContactHelper; @@ -285,8 +287,10 @@ public class CallDetailActivity extends Activity } }); - mContactInfoHelper = new ContactInfoHelper(this, GeoUtil.getCurrentCountryIso(this)); - mBlockContactHelper = new BlockContactHelper(this, new LookupProviderImpl(this)); + mBlockContactHelper = new BlockContactHelper(this); + mLookupProvider = LookupProviderImpl.INSTANCE.get(this); + mContactInfoHelper = new ContactInfoHelper(this, GeoUtil.getCurrentCountryIso(this), + mLookupProvider); getActionBar().setDisplayHomeAsUpEnabled(true); if (getIntent().getBooleanExtra(EXTRA_FROM_NOTIFICATION, false)) { @@ -297,6 +301,7 @@ public class CallDetailActivity extends Activity @Override protected void onDestroy() { super.onDestroy(); + LookupProviderImpl.INSTANCE.release(); mBlockContactHelper.destroy(); mCallRecordingDataStore.close(); } diff --git a/src/com/android/dialer/DialtactsActivity.java b/src/com/android/dialer/DialtactsActivity.java index ee8c6cab6..2557552a9 100644 --- a/src/com/android/dialer/DialtactsActivity.java +++ b/src/com/android/dialer/DialtactsActivity.java @@ -76,6 +76,7 @@ import com.android.contacts.common.list.OnPhoneNumberPickerActionListener; import com.android.contacts.common.util.PermissionsUtil; import com.android.contacts.common.widget.FloatingActionButtonController; import com.android.contacts.commonbind.analytics.AnalyticsUtil; +import com.android.dialer.callerinfo.CallerInfoProviderPicker; import com.android.dialer.calllog.CallLogActivity; import com.android.dialer.calllog.CallLogFragment; import com.android.dialer.database.DialerDatabaseHelper; @@ -640,6 +641,8 @@ public class DialtactsActivity extends TransactionSafeActivity implements View.O callStateIntentFilter.addAction(TelephonyIntents.ACTION_SUBINFO_CONTENT_CHANGE); callStateIntentFilter.addAction(TelephonyIntents.ACTION_SUBINFO_RECORD_UPDATED); registerReceiver(mCallStateReceiver, callStateIntentFilter); + + CallerInfoProviderPicker.onAppLaunched(this); } @Override diff --git a/src/com/android/dialer/callerinfo/CallerInfoProviderPicker.java b/src/com/android/dialer/callerinfo/CallerInfoProviderPicker.java index d8b0f02fa..918c67f22 100644 --- a/src/com/android/dialer/callerinfo/CallerInfoProviderPicker.java +++ b/src/com/android/dialer/callerinfo/CallerInfoProviderPicker.java @@ -14,140 +14,180 @@ package com.android.dialer.callerinfo; -import android.app.Activity; import android.app.AlertDialog; +import android.app.PendingIntent; import android.content.ComponentName; -import android.content.DialogInterface; +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; import android.content.res.Resources; -import android.os.Bundle; +import android.graphics.Bitmap; +import android.preference.PreferenceManager; +import android.provider.Settings; import android.text.Html; -import android.text.method.LinkMovementMethod; -import android.view.View; -import android.widget.ImageView; -import android.widget.TextView; + import com.android.dialer.R; -import com.android.dialer.util.MetricsHelper; +import com.android.dialer.util.ImageUtils; import com.android.phone.common.ambient.AmbientConnection; import com.cyanogen.ambient.callerinfo.util.CallerInfoHelper; import com.cyanogen.ambient.callerinfo.util.ProviderInfo; -import com.cyanogen.ambient.callerinfo.CallerInfoServices; +import com.cyanogen.ambient.common.CyanogenAmbientUtil; +import com.cyanogen.ambient.common.api.ResultCallback; +import com.cyanogen.ambient.discovery.DiscoveryManagerServices; +import com.cyanogen.ambient.discovery.nudge.DialogNudge; +import com.cyanogen.ambient.discovery.results.BooleanResult; + +public class CallerInfoProviderPicker { + private static final String NUDGE_ID = "callerInfoPickerDialogNudge"; -public class CallerInfoProviderPicker extends Activity { + private static final int REQUEST_CODE_SUCCESS = 0; + private static final int REQUEST_CODE_FAILURE = 1; - private static final String TAG = "CallerInfoProviderPicker"; + private static final String PREF_FIRST_LAUNCH = "first_launch"; + private static final String PREF_UNKNOWN_CALL_COUNT = "unknown_call_count"; + private static final int UNKNOWN_CALL_FIRST_COUNT = 0; + private static final int UNKNOWN_CALL_FINAL_COUNT = 4; - public static final String EXTRA_PROVIDER_INFO = "extra_provider_info"; - public static final String EXTRA_METRICS_REASON = "extra_reason"; - public static final int REASON_FIRST_LAUNCH_DIALER = 0; - public static final int REASON_INCOMING_CALL = 1; - public static final int REASON_INCOMING_CALL_FINAL_PROMPT = 2; - public static final int REASON_DIALER_SETTINGS = 3; + public static void onAppLaunched(Context context) { + if (checkPreconditions(context)) { + final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + if (prefs.getBoolean(PREF_FIRST_LAUNCH, true)) { + enableProvider(context, true, ProviderActivationService.REASON_FIRST_LAUNCH_DIALER, + new ResultCallback<BooleanResult>() { + @Override + public void onResult(BooleanResult result) { + // Don't count this as first launch, + // unless discovery actually shows our dialog. + if (result.bool) { + prefs.edit().putBoolean(PREF_FIRST_LAUNCH, false).commit(); + } + } + } + ); + } + } + } - private AlertDialog mDialog; + public static void onUnknownCallEnded(Context context) { + if (checkPreconditions(context)) { + final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + final int currentCount = prefs.getInt(PREF_UNKNOWN_CALL_COUNT, 0); - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); + int metricsReason; + if (currentCount == UNKNOWN_CALL_FIRST_COUNT) { + metricsReason = ProviderActivationService.REASON_INCOMING_CALL_FIRST_PROMPT; + } else if (currentCount == UNKNOWN_CALL_FINAL_COUNT) { + metricsReason = ProviderActivationService.REASON_INCOMING_CALL_FINAL_PROMPT; + } else { + prefs.edit().putInt(PREF_UNKNOWN_CALL_COUNT, currentCount + 1).commit(); + return; + } + + enableProvider(context, true, metricsReason, + new ResultCallback<BooleanResult>() { + @Override + public void onResult(BooleanResult result) { + // Don't count this event, unless discovery actually shows our dialog. + if (result.bool) { + prefs.edit().putInt(PREF_UNKNOWN_CALL_COUNT, currentCount + 1). + commit(); + } + } + }); + } + } - final ComponentName provider = getIntent().getParcelableExtra(EXTRA_PROVIDER_INFO); - final ProviderInfo providerInfo = CallerInfoHelper.getProviderInfo(this, provider); - if (providerInfo == null) { - finish(); + public static void onSettingEnabled(Context context) { + enableProvider(context, false, ProviderActivationService.REASON_DIALER_SETTINGS, null); + } + + private static void enableProvider(Context context, boolean withDialog, int metricsReason, + ResultCallback<BooleanResult> callback) { + CallerInfoHelper.ResolvedProvider[] providers = + CallerInfoHelper.getInstalledProviders(context); + if (providers.length == 0) { return; } - final Resources res = getResources(); - View view = View.inflate(this, R.layout.callerinfo_provider_picker, null); - ImageView logo = (ImageView) view.findViewById(R.id.logo); - TextView description = (TextView) view.findViewById(R.id.description); - TextView disclaimer = (TextView) view.findViewById(R.id.disclaimer); + // Assume only one provider + ComponentName component = providers[0].getComponent(); + ProviderInfo info = CallerInfoHelper.getProviderInfo(context, component); + + if (withDialog) { + showDialog(context, metricsReason, component, info, callback); + } else { + context.startService(buildEnableIntent(context, component, info, metricsReason)); + } + } + + private static boolean checkPreconditions(Context context) { + boolean setupCompleted = Settings.Secure.getInt(context.getContentResolver(), + Settings.Secure.USER_SETUP_COMPLETE, 0) != 0; + boolean ambientAvailable = CyanogenAmbientUtil.isCyanogenAmbientAvailable(context) + == CyanogenAmbientUtil.SUCCESS; + boolean providerEnabled = CallerInfoHelper.getActiveProviderInfo2(context) != null; + boolean providerAvailable = CallerInfoHelper.getInstalledProviders(context).length > 0; + return setupCompleted && ambientAvailable && !providerEnabled && providerAvailable; + } - if (providerInfo.hasProperty(ProviderInfo.PROPERTY_NEEDS_CONTACTS)) { - String text = res.getString(R.string.callerinfo_provider_auth_access, providerInfo.getTitle()); - if (providerInfo.getPrivacyPolicyUrl() != null) { + private static void showDialog(Context context, int metricsReason, ComponentName component, + ProviderInfo info, ResultCallback<BooleanResult> callback) { + Resources res = context.getResources(); + CharSequence subText = null; + if (info.hasProperty(ProviderInfo.PROPERTY_NEEDS_CONTACTS)) { + String text = res.getString(R.string.callerinfo_provider_auth_access, info.getTitle()); + if (info.getPrivacyPolicyUrl() != null) { String learnMore = " <a href=\"%s\">%s</a>"; - text += String.format(learnMore, providerInfo.getPrivacyPolicyUrl(), + text += String.format(learnMore, info.getPrivacyPolicyUrl(), res.getString(R.string.callerinfo_provider_auth_learn_more)); } - disclaimer.setMovementMethod(LinkMovementMethod.getInstance()); - disclaimer.setText(Html.fromHtml(text)); - } else { - disclaimer.setVisibility(View.GONE); + subText = Html.fromHtml(text); } - logo.setImageDrawable(providerInfo.getBrandLogo()); - - int resId = providerInfo.hasProperty(ProviderInfo.PROPERTY_SUPPORTS_SPAM) ? - R.string.callerinfo_provider_auth_desc : R.string.callerinfo_provider_auth_desc_no_spam; - description.setText(res.getString(resId, providerInfo.getTitle())); - - AlertDialog.Builder builder = new AlertDialog.Builder(this); - builder.setView(view); - builder.setPositiveButton(res.getString(R.string.callerinfo_provider_auth_yes), - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - sendMetrics(true, providerInfo.getPackageName()); - CallerInfoServices.CallerInfoApi.enablePlugin( - AmbientConnection.CLIENT.get(getApplicationContext()), - providerInfo.getComponentName()); - finish(); - } - }).setNegativeButton(res.getString(R.string.callerinfo_provider_auth_no), - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - sendMetrics(false, providerInfo.getPackageName()); - finish(); - } - }).setOnDismissListener(new DialogInterface.OnDismissListener() { - @Override - public void onDismiss(DialogInterface dialog) { - finish(); - } - }); - mDialog = builder.create(); - mDialog.setOnShowListener(new DialogInterface.OnShowListener() { - @Override - public void onShow(DialogInterface dialog) { - int buttonColor = res.getColor(R.color.callerinfo_provider_picker_negative_color); - mDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(buttonColor); - } - }); - mDialog.show(); - } + Bitmap logo = ImageUtils.drawableToBitmap(info.getBrandLogo()); - @Override - public void onStop() { - super.onStop(); - } + int resId = info.hasProperty(ProviderInfo.PROPERTY_SUPPORTS_SPAM) ? + R.string.callerinfo_provider_auth_desc : + R.string.callerinfo_provider_auth_desc_no_spam; + String bodyText = res.getString(resId, info.getTitle()); - void sendMetrics(boolean onAccept, String providerPackageName) { - if (!getIntent().hasExtra(EXTRA_METRICS_REASON)) { - return; + DialogNudge nudge = new DialogNudge(NUDGE_ID, DialogNudge.SubheadPosition.BOTTOM, + info.getTitle(), bodyText); + + if (subText != null) { + nudge.setSubhead(subText.toString()); } - int reason = getIntent().getIntExtra(EXTRA_METRICS_REASON, -1); - MetricsHelper.Field field = new MetricsHelper.Field( - MetricsHelper.Fields.PROVIDER_PACKAGE_NAME, providerPackageName); - MetricsHelper.Actions action = onAccept ? - MetricsHelper.Actions.OPTED_IN : MetricsHelper.Actions.OPTED_OUT; - switch (reason) { - case REASON_FIRST_LAUNCH_DIALER: - MetricsHelper.sendEvent(MetricsHelper.Categories.PROVIDER_STATE_CHANGES, - action, MetricsHelper.State.FIRST_LAUNCH_DIALER, field); - break; - case REASON_INCOMING_CALL_FINAL_PROMPT: - MetricsHelper.sendEvent(MetricsHelper.Categories.PROVIDER_STATE_CHANGES, - action, MetricsHelper.State.AFTER_FINAL_PROMPT, field); - break; - case REASON_INCOMING_CALL: - MetricsHelper.sendEvent(MetricsHelper.Categories.PROVIDER_STATE_CHANGES, - action, MetricsHelper.State.AFTER_CALL_ENDED, field); - break; - case REASON_DIALER_SETTINGS: - MetricsHelper.sendEvent(MetricsHelper.Categories.PROVIDER_STATE_CHANGES, - action, MetricsHelper.State.SETTINGS, field); - break; + + if (logo != null) { + nudge.setTitleImage(logo); } + + Intent enableIntent = buildEnableIntent(context, component, info, metricsReason); + PendingIntent positivePendingIntent = PendingIntent.getService(context, + REQUEST_CODE_SUCCESS, enableIntent, PendingIntent.FLAG_CANCEL_CURRENT); + + Intent cancelIntent = (Intent) enableIntent.clone(); + cancelIntent.putExtra(ProviderActivationService.INTENT_EXTRA_SUCCESS, false); + PendingIntent negativePendingIntent = PendingIntent.getService(context, + REQUEST_CODE_FAILURE, cancelIntent, PendingIntent.FLAG_CANCEL_CURRENT); + + nudge.addButton(new DialogNudge.Button(res.getString(R.string.callerinfo_provider_auth_yes), + AlertDialog.BUTTON_POSITIVE, positivePendingIntent)); + nudge.addButton(new DialogNudge.Button(res.getString(R.string.callerinfo_provider_auth_no), + AlertDialog.BUTTON_NEGATIVE, negativePendingIntent)); + + DiscoveryManagerServices.DiscoveryManagerApi. + publishNudge(AmbientConnection.CLIENT.get(context), nudge). + setResultCallback(callback); + } + + private static Intent buildEnableIntent(Context context, ComponentName component, + ProviderInfo info, int metricsReason) { + Intent intent = new Intent(context, ProviderActivationService.class); + intent.putExtra(ProviderActivationService.INTENT_EXTRA_PACKAGE, info.getPackageName()); + intent.putExtra(ProviderActivationService.INTENT_EXTRA_COMPONENT, component); + intent.putExtra(ProviderActivationService.INTENT_EXTRA_METRIC_REASON, metricsReason); + intent.putExtra(ProviderActivationService.INTENT_EXTRA_SUCCESS, true); + return intent; } } diff --git a/src/com/android/dialer/callerinfo/ProviderActivationService.java b/src/com/android/dialer/callerinfo/ProviderActivationService.java new file mode 100644 index 000000000..1737bc901 --- /dev/null +++ b/src/com/android/dialer/callerinfo/ProviderActivationService.java @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2016 The CyanogenMod Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package com.android.dialer.callerinfo; + +import android.app.IntentService; +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; + +import com.android.dialer.util.MetricsHelper; +import com.android.phone.common.ambient.AmbientConnection; +import com.cyanogen.ambient.callerinfo.CallerInfoServices; +import com.cyanogen.ambient.common.api.AmbientApiClient; + +public class ProviderActivationService extends IntentService { + public static final String INTENT_EXTRA_PACKAGE = "package"; + public static final String INTENT_EXTRA_COMPONENT = "component"; + public static final String INTENT_EXTRA_SUCCESS = "success"; + public static final String INTENT_EXTRA_METRIC_REASON = "metric_reason"; + + public static final int REASON_FIRST_LAUNCH_DIALER = 0; + public static final int REASON_INCOMING_CALL_FIRST_PROMPT = 1; + public static final int REASON_INCOMING_CALL_FINAL_PROMPT = 2; + public static final int REASON_DIALER_SETTINGS = 3; + + private static final String TAG = ProviderActivationService.class.getSimpleName(); + + public ProviderActivationService() { + super(TAG); + } + + @Override + protected void onHandleIntent(final Intent intent) { + boolean success = intent.getBooleanExtra(INTENT_EXTRA_SUCCESS, false); + sendMetrics(intent, success); + + if (success) { + connectToProvider(getApplicationContext(), intent); + } + } + + private static void connectToProvider(final Context context, Intent intent) { + final ComponentName provider = intent.getParcelableExtra(INTENT_EXTRA_COMPONENT); + AmbientApiClient client = AmbientConnection.CLIENT.get(context); + CallerInfoServices.CallerInfoApi.enablePlugin(client, provider); + } + + private static void sendMetrics(Intent intent, boolean success) { + int reason = intent.getIntExtra(INTENT_EXTRA_METRIC_REASON, -1); + MetricsHelper.Field field = new MetricsHelper.Field( + MetricsHelper.Fields.PROVIDER_PACKAGE_NAME, + intent.getStringExtra(INTENT_EXTRA_PACKAGE)); + MetricsHelper.Actions action = success ? + MetricsHelper.Actions.OPTED_IN : MetricsHelper.Actions.OPTED_OUT; + MetricsHelper.State state = null; + switch (reason) { + case REASON_FIRST_LAUNCH_DIALER: + state = MetricsHelper.State.FIRST_LAUNCH_DIALER; + break; + case REASON_INCOMING_CALL_FIRST_PROMPT: + state = MetricsHelper.State.AFTER_CALL_ENDED; + break; + case REASON_INCOMING_CALL_FINAL_PROMPT: + state = MetricsHelper.State.AFTER_FINAL_PROMPT; + break; + case REASON_DIALER_SETTINGS: + state = MetricsHelper.State.SETTINGS; + break; + } + + MetricsHelper.sendEvent(MetricsHelper.Categories.PROVIDER_STATE_CHANGES, + action, state, field); + } +}
\ No newline at end of file diff --git a/src/com/android/dialer/calllog/BlockContactPresenter.java b/src/com/android/dialer/calllog/BlockContactPresenter.java index 47810dca9..202774d80 100644 --- a/src/com/android/dialer/calllog/BlockContactPresenter.java +++ b/src/com/android/dialer/calllog/BlockContactPresenter.java @@ -50,7 +50,7 @@ public class BlockContactPresenter implements View.OnClickListener, view.setOnClickListener(this); mViewHolder = BlockContactViewHolder.create(view, mViewHolder); if (mBlockContactHelper == null) { - mBlockContactHelper = new BlockContactHelper(mActivity, new LookupProviderImpl(mActivity)); + mBlockContactHelper = new BlockContactHelper(mActivity); mBlockContactHelper.setStatusListener(this); } mBlockContactHelper.setContactInfo(number); diff --git a/src/com/android/dialer/calllog/CallLogAsyncTaskUtil.java b/src/com/android/dialer/calllog/CallLogAsyncTaskUtil.java index 63f79be0b..5a5682cab 100644 --- a/src/com/android/dialer/calllog/CallLogAsyncTaskUtil.java +++ b/src/com/android/dialer/calllog/CallLogAsyncTaskUtil.java @@ -39,6 +39,7 @@ import com.android.dialer.util.TelecomUtil; import com.cyanogen.ambient.incall.CallLogConstants; +import com.cyanogen.lookup.phonenumber.provider.LookupProviderImpl; import com.google.common.annotations.VisibleForTesting; public class CallLogAsyncTaskUtil { @@ -162,17 +163,18 @@ public class CallLogAsyncTaskUtil { countryIso, cursor.getString(CallDetailQuery.PLUGIN_PACKAGE_NAME)); // If this is not a regular number, there is no point in looking it up in the contacts. - ContactInfoHelper contactInfoHelper = - new ContactInfoHelper(context, GeoUtil.getCurrentCountryIso(context)); boolean isVoicemail = PhoneNumberUtil.isVoicemailNumber(context, accountHandle, number); boolean shouldLookupNumber = PhoneNumberUtil.canPlaceCallsTo(number, numberPresentation) && !isVoicemail; ContactInfo info = ContactInfo.EMPTY; if (shouldLookupNumber) { - ContactInfo lookupInfo = - contactInfoHelper.lookupNumber(number, countryIso, isInCallPluginContactId); + ContactInfoHelper contactInfoHelper = + new ContactInfoHelper(context, GeoUtil.getCurrentCountryIso(context), + LookupProviderImpl.INSTANCE.get(context)); + ContactInfo lookupInfo = contactInfoHelper.lookupNumber(number, countryIso, false); info = lookupInfo != null ? lookupInfo : ContactInfo.EMPTY; + LookupProviderImpl.INSTANCE.release(); } PhoneCallDetails details = new PhoneCallDetails( diff --git a/src/com/android/dialer/calllog/CallLogFragment.java b/src/com/android/dialer/calllog/CallLogFragment.java index 1f223b970..0c6b69fae 100644 --- a/src/com/android/dialer/calllog/CallLogFragment.java +++ b/src/com/android/dialer/calllog/CallLogFragment.java @@ -54,6 +54,8 @@ import com.cyanogen.ambient.common.api.ResultCallback; import com.cyanogen.ambient.deeplink.DeepLink; import com.cyanogen.ambient.deeplink.applicationtype.DeepLinkApplicationType; import com.cyanogen.ambient.incall.CallLogConstants; +import com.cyanogen.lookup.phonenumber.contract.LookupProvider; +import com.cyanogen.lookup.phonenumber.provider.LookupProviderImpl; import java.util.HashMap; @@ -63,7 +65,7 @@ import java.util.HashMap; */ public class CallLogFragment extends Fragment implements CallLogQueryHandler.Listener, CallLogAdapter.CallFetcher, OnEmptyViewActionButtonClickedListener, - BlockContactDialogFragment.Callbacks { + BlockContactDialogFragment.Callbacks, LookupProvider.StatusCallback { private static final String TAG = "CallLogFragment"; /** @@ -87,6 +89,7 @@ public class CallLogFragment extends Fragment implements CallLogQueryHandler.Lis private CallLogAdapter mAdapter; private CallLogQueryHandler mCallLogQueryHandler; private VoicemailPlaybackPresenter mVoicemailPlaybackPresenter; + private LookupProvider mLookupProvider; private boolean mScrollToTop; /** Whether there is at least one voicemail source installed. */ @@ -243,10 +246,12 @@ public class CallLogFragment extends Fragment implements CallLogQueryHandler.Lis mBlockContactPresenter = new BlockContactPresenter(activity, this); boolean isShowingRecentsTab = mLogLimit != NO_LOG_LIMIT || mDateLimit != NO_DATE_LIMIT; + mLookupProvider = LookupProviderImpl.INSTANCE.get(getActivity()); + mLookupProvider.registerStatusCallback(this); mAdapter = ObjectFactory.newCallLogAdapter( getActivity(), this, - new ContactInfoHelper(getActivity(), currentCountryIso), + new ContactInfoHelper(getActivity(), currentCountryIso, mLookupProvider), mVoicemailPlaybackPresenter, mBlockContactPresenter, isShowingRecentsTab); @@ -418,6 +423,8 @@ public class CallLogFragment extends Fragment implements CallLogQueryHandler.Lis getActivity().getContentResolver().unregisterContentObserver(mCallLogObserver); getActivity().getContentResolver().unregisterContentObserver(mContactsObserver); getActivity().getContentResolver().unregisterContentObserver(mVoicemailStatusObserver); + mLookupProvider.unregisterStatusCallback(this); + LookupProviderImpl.INSTANCE.release(); super.onDestroy(); } @@ -436,6 +443,12 @@ public class CallLogFragment extends Fragment implements CallLogQueryHandler.Lis } @Override + public void onStatusChanged(boolean enabled) { + mRefreshDataRequired = true; + refreshData(); + } + + @Override public void fetchCalls() { mCallLogQueryHandler.fetchCalls(mCallTypeFilter, mDateLimit); } diff --git a/src/com/android/dialer/calllog/ContactInfoHelper.java b/src/com/android/dialer/calllog/ContactInfoHelper.java index 3442df0af..ff0dee5d5 100644 --- a/src/com/android/dialer/calllog/ContactInfoHelper.java +++ b/src/com/android/dialer/calllog/ContactInfoHelper.java @@ -73,15 +73,11 @@ public class ContactInfoHelper { private static final CachedNumberLookupService mCachedNumberLookupService = ObjectFactory.newCachedNumberLookupService(); - public ContactInfoHelper(Context context, String currentCountryIso) { + public ContactInfoHelper(Context context, String currentCountryIso, + LookupProvider lookupProvider) { mContext = context; mCurrentCountryIso = currentCountryIso; - LookupProvider lookupProvider = new LookupProviderImpl(context); - if (lookupProvider.initialize()) { - mLookupProvider = lookupProvider; - } else { - mLookupProvider = null; - } + mLookupProvider = lookupProvider; } /** @@ -121,6 +117,11 @@ public class ContactInfoHelper { ContactInfo phoneInfo = queryContactInfoForPhoneNumber(number, countryIso, isInCallPluginContactId); + // If we got a result, but the data is invalid, bail out and try again later. + if (phoneInfo != null && phoneInfo.isBadData) { + return null; + } + if (phoneInfo == null || phoneInfo == ContactInfo.EMPTY) { // Check whether the phone number has been saved as an "Internet call" number. phoneInfo = queryContactInfoForSipAddress(number); @@ -314,36 +315,41 @@ public class ContactInfoHelper { } } // always do a LookupProvider search, if available, for a non-contact - if (mLookupProvider != null && !isLocalContact) { + if (mLookupProvider.isEnabled() && !isLocalContact) { LookupResponse response = mLookupProvider.blockingFetchInfo( new LookupRequest(PhoneNumberUtils.formatNumberToE164(number, countryIso), null, LookupRequest.RequestOrigin.OTHER) ); - if (response != null && response.mStatusCode == StatusCode.SUCCESS) { - logSuccessfulFetch(); - final String formattedNumber = formatPhoneNumber(response.mNumber, null, countryIso); - // map LookupResponse to ContactInfo - ContactInfo contactInfo = new ContactInfo(); - contactInfo.lookupProviderName = response.mProviderName; - contactInfo.name = response.mName; - contactInfo.number = formatPhoneNumber(response.mNumber, null, countryIso); - contactInfo.city = response.mCity; - contactInfo.country = response.mCountry; - contactInfo.address = response.mAddress; - contactInfo.photoUrl = response.mPhotoUrl; - contactInfo.isSpam = response.mIsSpam; - contactInfo.spamCount = response.mSpamCount; - contactInfo.attributionDrawable = response.mAttributionLogo; - - // construct encoded lookup uri - ContactBuilder contactBuilder = new ContactBuilder(ContactBuilder.REVERSE_LOOKUP, - response.mNumber, formattedNumber); - contactBuilder.setInfoProviderName(response.mProviderName); - contactBuilder.setPhotoUrl(response.mPhotoUrl); - contactBuilder.setName(ContactBuilder.Name.createDisplayName(response.mName)); - - contactInfo.lookupUri = contactBuilder.build().lookupUri; - info = contactInfo; + + if (response != null) { + if (response.mStatusCode == StatusCode.FAIL) { + info.isBadData = true; + } else if (response.mStatusCode == StatusCode.SUCCESS) { + logSuccessfulFetch(); + final String formattedNumber = formatPhoneNumber(response.mNumber, null, countryIso); + // map LookupResponse to ContactInfo + ContactInfo contactInfo = new ContactInfo(); + contactInfo.lookupProviderName = response.mProviderName; + contactInfo.name = response.mName; + contactInfo.number = formatPhoneNumber(response.mNumber, null, countryIso); + contactInfo.city = response.mCity; + contactInfo.country = response.mCountry; + contactInfo.address = response.mAddress; + contactInfo.photoUrl = response.mPhotoUrl; + contactInfo.isSpam = response.mIsSpam; + contactInfo.spamCount = response.mSpamCount; + contactInfo.attributionDrawable = response.mAttributionLogo; + + // construct encoded lookup uri + ContactBuilder contactBuilder = new ContactBuilder(ContactBuilder.REVERSE_LOOKUP, + response.mNumber, formattedNumber); + contactBuilder.setInfoProviderName(response.mProviderName); + contactBuilder.setPhotoUrl(response.mPhotoUrl); + contactBuilder.setName(ContactBuilder.Name.createDisplayName(response.mName)); + + contactInfo.lookupUri = contactBuilder.build().lookupUri; + info = contactInfo; + } } } return info; diff --git a/src/com/android/dialer/callstats/CallStatsAdapter.java b/src/com/android/dialer/callstats/CallStatsAdapter.java index 672fa6f83..a2ecbf763 100644 --- a/src/com/android/dialer/callstats/CallStatsAdapter.java +++ b/src/com/android/dialer/callstats/CallStatsAdapter.java @@ -39,6 +39,8 @@ import com.android.dialer.calllog.ContactInfoHelper; import com.android.dialer.contactinfo.ContactInfoCache; import com.android.dialer.util.DialerUtils; import com.android.dialer.util.PhoneNumberUtil; +import com.cyanogen.lookup.phonenumber.contract.LookupProvider; +import com.cyanogen.lookup.phonenumber.provider.LookupProviderImpl; import java.util.ArrayList; import java.util.Collections; @@ -53,6 +55,7 @@ class CallStatsAdapter extends RecyclerView.Adapter { private final Context mContext; private final ContactInfoHelper mContactInfoHelper; private final ContactInfoCache mContactInfoCache; + private final LookupProvider mLookupProvider; private ArrayList<CallStatsDetails> mAllItems; private ArrayList<CallStatsDetails> mShownItems; @@ -159,7 +162,8 @@ class CallStatsAdapter extends RecyclerView.Adapter { mContext = context; final String currentCountryIso = GeoUtil.getCurrentCountryIso(mContext); - mContactInfoHelper = new ContactInfoHelper(mContext, currentCountryIso); + mLookupProvider = LookupProviderImpl.INSTANCE.get(mContext); + mContactInfoHelper = new ContactInfoHelper(mContext, currentCountryIso, mLookupProvider); mAllItems = new ArrayList<CallStatsDetails>(); mShownItems = new ArrayList<CallStatsDetails>(); @@ -230,7 +234,8 @@ class CallStatsAdapter extends RecyclerView.Adapter { public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { LayoutInflater inflater = LayoutInflater.from(mContext); View view = inflater.inflate(R.layout.call_stats_list_item, parent, false); - CallStatsListItemViewHolder viewHolder = CallStatsListItemViewHolder.create(view); + CallStatsListItemViewHolder viewHolder = CallStatsListItemViewHolder.create(view, + mContactInfoHelper); viewHolder.mPrimaryActionView.setOnCreateContextMenuListener(mOnCreateContextMenuListener); viewHolder.mPrimaryActionView.setTag(viewHolder); @@ -267,4 +272,8 @@ class CallStatsAdapter extends RecyclerView.Adapter { return CallStatsListItemViewHolder.getDurationString( mContext, duration, withSeconds); } + + public void destroy() { + LookupProviderImpl.INSTANCE.release(); + } } diff --git a/src/com/android/dialer/callstats/CallStatsDetailActivity.java b/src/com/android/dialer/callstats/CallStatsDetailActivity.java index 1be6294b4..df22f28b5 100644 --- a/src/com/android/dialer/callstats/CallStatsDetailActivity.java +++ b/src/com/android/dialer/callstats/CallStatsDetailActivity.java @@ -48,6 +48,7 @@ import com.android.dialer.calllog.ContactInfoHelper; import com.android.dialer.util.IntentUtil; import com.android.dialer.util.PhoneNumberUtil; import com.android.dialer.widget.LinearColorBar; +import com.cyanogen.lookup.phonenumber.contract.LookupProvider; import com.cyanogen.lookup.phonenumber.provider.LookupProviderImpl; /** @@ -87,6 +88,8 @@ public class CallStatsDetailActivity extends Activity implements private String mNumber = null; private boolean mHasEditNumberBeforeCallOption; + private LookupProvider mLookupProvider; + private class UpdateContactTask extends AsyncTask<String, Void, ContactInfo> { @Override protected ContactInfo doInBackground(String... strings) { @@ -111,8 +114,10 @@ public class CallStatsDetailActivity extends Activity implements setContentView(R.layout.call_stats_detail); mResources = getResources(); - mContactInfoHelper = new ContactInfoHelper(this, GeoUtil.getCurrentCountryIso(this)); - mBlockContactHelper = new BlockContactHelper(this, new LookupProviderImpl(this)); + mLookupProvider = LookupProviderImpl.INSTANCE.get(this); + mContactInfoHelper = new ContactInfoHelper(this, GeoUtil.getCurrentCountryIso(this), + mLookupProvider); + mBlockContactHelper = new BlockContactHelper(this); mQuickContactBadge = (QuickContactBadge) findViewById(R.id.quick_contact_photo); mQuickContactBadge.setOverlay(null); @@ -301,6 +306,11 @@ public class CallStatsDetailActivity extends Activity implements } @Override + protected void onDestroy() { + LookupProviderImpl.INSTANCE.release(); + } + + @Override public boolean onPrepareOptionsMenu(Menu menu) { menu.findItem(R.id.menu_edit_number_before_call) .setVisible(mHasEditNumberBeforeCallOption); diff --git a/src/com/android/dialer/callstats/CallStatsFragment.java b/src/com/android/dialer/callstats/CallStatsFragment.java index abccf9b38..5d739fa51 100644 --- a/src/com/android/dialer/callstats/CallStatsFragment.java +++ b/src/com/android/dialer/callstats/CallStatsFragment.java @@ -319,6 +319,7 @@ public class CallStatsFragment extends Fragment implements public void onDestroy() { super.onDestroy(); mAdapter.pauseCache(); + mAdapter.destroy(); getActivity().getContentResolver().unregisterContentObserver(mObserver); } diff --git a/src/com/android/dialer/callstats/CallStatsListItemViewHolder.java b/src/com/android/dialer/callstats/CallStatsListItemViewHolder.java index 97d5084f0..6c5269dc6 100644 --- a/src/com/android/dialer/callstats/CallStatsListItemViewHolder.java +++ b/src/com/android/dialer/callstats/CallStatsListItemViewHolder.java @@ -70,7 +70,8 @@ public final class CallStatsListItemViewHolder extends RecyclerView.ViewHolder TextView numberView, TextView labelView, TextView percentView, - LinearColorBar barView) { + LinearColorBar barView, + ContactInfoHelper contactInfoHelper) { super(rootView); mRootView = rootView; @@ -88,11 +89,11 @@ public final class CallStatsListItemViewHolder extends RecyclerView.ViewHolder mContext = rootView.getContext(); mPhotoSize = mContext.getResources().getDimensionPixelSize(R.dimen.contact_photo_size); - mContactInfoHelper = new ContactInfoHelper(mContext, - GeoUtil.getCurrentCountryIso(mContext)); + mContactInfoHelper = contactInfoHelper; } - public static CallStatsListItemViewHolder create(View view) { + public static CallStatsListItemViewHolder create(View view, + ContactInfoHelper contactInfoHelper) { return new CallStatsListItemViewHolder(view, (QuickContactBadge) view.findViewById(R.id.quick_contact_photo), view.findViewById(R.id.primary_action_view), @@ -100,7 +101,8 @@ public final class CallStatsListItemViewHolder extends RecyclerView.ViewHolder (TextView) view.findViewById(R.id.number), (TextView) view.findViewById(R.id.label), (TextView) view.findViewById(R.id.percent), - (LinearColorBar) view.findViewById(R.id.percent_bar)); + (LinearColorBar) view.findViewById(R.id.percent_bar), + contactInfoHelper); } @Override diff --git a/src/com/android/dialer/settings/DialerSettingsActivity.java b/src/com/android/dialer/settings/DialerSettingsActivity.java index 39a79cfdf..9422ce028 100644 --- a/src/com/android/dialer/settings/DialerSettingsActivity.java +++ b/src/com/android/dialer/settings/DialerSettingsActivity.java @@ -498,12 +498,7 @@ public class DialerSettingsActivity extends PreferenceActivity { // do nothing } else { // Activate the provider - Intent i = new Intent(getContext(), CallerInfoProviderPicker.class); - i.putExtra(CallerInfoProviderPicker.EXTRA_PROVIDER_INFO, - mCallerInfoProvider.getComponentName()); - i.putExtra(CallerInfoProviderPicker.EXTRA_METRICS_REASON, - CallerInfoProviderPicker.REASON_DIALER_SETTINGS); - ((Activity) getContext()).startActivityForResult(i, 0); + CallerInfoProviderPicker.onSettingEnabled(getContext()); } } else if (header.titleRes == R.string.silence_spam_title) { if (status == com.cyanogen.ambient.callerinfo.util.PluginStatus.ACTIVE) { |
