1 /* 2 * Copyright (C) 2018 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.promotion.impl; 18 19 import android.content.Context; 20 import android.content.SharedPreferences; 21 import android.support.annotation.VisibleForTesting; 22 import com.android.dialer.configprovider.ConfigProvider; 23 import com.android.dialer.duo.Duo; 24 import com.android.dialer.inject.ApplicationContext; 25 import com.android.dialer.promotion.Promotion; 26 import com.android.dialer.spannable.ContentWithLearnMoreSpanner; 27 import com.android.dialer.storage.Unencrypted; 28 import java.util.concurrent.TimeUnit; 29 import javax.inject.Inject; 30 31 /** Duo promotion. */ 32 final class DuoPromotion implements Promotion { 33 private static final String SHARED_PREF_KEY_DUO_DISCLOSURE_DISMISSED = "duo_disclosure_dismissed"; 34 35 private static final String SHARED_PREF_KEY_DUO_DISCLOSURE_FIRST_VIEW_TIME_MILLIS = 36 "duo_disclosure_first_viewed_time_ms"; 37 @VisibleForTesting static final String FLAG_SHOW_DUO_DISCLOSURE = "show_duo_disclosure"; 38 39 @VisibleForTesting 40 static final String FLAG_DUO_DISCLOSURE_AUTO_DISMISS_AFTER_VIEWED_TIME_MILLIS = 41 "show_duo_disclosure_auto_dismiss_after_viewed_time_millis"; 42 43 private final Context appContext; 44 private final ConfigProvider configProvider; 45 private final Duo duo; 46 private final SharedPreferences sharedPreferences; 47 48 @Inject DuoPromotion( @pplicationContext Context context, ConfigProvider configProvider, Duo duo, @Unencrypted SharedPreferences sharedPreferences)49 DuoPromotion( 50 @ApplicationContext Context context, 51 ConfigProvider configProvider, 52 Duo duo, 53 @Unencrypted SharedPreferences sharedPreferences) { 54 this.appContext = context; 55 this.configProvider = configProvider; 56 this.duo = duo; 57 this.sharedPreferences = sharedPreferences; 58 } 59 60 @Override getType()61 public int getType() { 62 return PromotionType.CARD; 63 } 64 65 @Override isEligibleToBeShown()66 public boolean isEligibleToBeShown() { 67 if (!configProvider.getBoolean(FLAG_SHOW_DUO_DISCLOSURE, false)) { 68 return false; 69 } 70 // Don't show the Duo disclosure card if 71 // (1) Duo integration is not enabled on the device, or 72 // (2) Duo is not activated. 73 if (!duo.isEnabled(appContext) || !duo.isActivated(appContext)) { 74 return false; 75 } 76 77 // Don't show the Duo disclosure card if it has been dismissed. 78 if (sharedPreferences.getBoolean(SHARED_PREF_KEY_DUO_DISCLOSURE_DISMISSED, false)) { 79 return false; 80 } 81 82 // At this point, Duo is activated and the disclosure card hasn't been dismissed. 83 // We should show the card if it has never been viewed by the user. 84 if (!sharedPreferences.contains(SHARED_PREF_KEY_DUO_DISCLOSURE_FIRST_VIEW_TIME_MILLIS)) { 85 return true; 86 } 87 88 // At this point, the card has been viewed but not dismissed. 89 // We should not show the card if it has been viewed for more than 1 day. 90 long duoDisclosureFirstViewTimeMillis = 91 sharedPreferences.getLong(SHARED_PREF_KEY_DUO_DISCLOSURE_FIRST_VIEW_TIME_MILLIS, 0); 92 return System.currentTimeMillis() - duoDisclosureFirstViewTimeMillis 93 <= configProvider.getLong( 94 FLAG_DUO_DISCLOSURE_AUTO_DISMISS_AFTER_VIEWED_TIME_MILLIS, TimeUnit.DAYS.toMillis(1)); 95 } 96 97 @Override onViewed()98 public void onViewed() { 99 if (!sharedPreferences.contains(SHARED_PREF_KEY_DUO_DISCLOSURE_FIRST_VIEW_TIME_MILLIS)) { 100 sharedPreferences 101 .edit() 102 .putLong( 103 SHARED_PREF_KEY_DUO_DISCLOSURE_FIRST_VIEW_TIME_MILLIS, System.currentTimeMillis()) 104 .apply(); 105 } 106 } 107 108 @Override dismiss()109 public void dismiss() { 110 sharedPreferences.edit().putBoolean(SHARED_PREF_KEY_DUO_DISCLOSURE_DISMISSED, true).apply(); 111 } 112 113 @Override getTitle()114 public CharSequence getTitle() { 115 return appContext.getString(R.string.duo_disclosure_title); 116 } 117 118 @Override getDetails()119 public CharSequence getDetails() { 120 return new ContentWithLearnMoreSpanner(appContext) 121 .create( 122 appContext.getString(R.string.duo_disclosure_details), 123 configProvider.getString( 124 "duo_disclosure_link_full_url", 125 "http://support.google.com/pixelphone/?p=dialer_duo")); 126 } 127 128 @Override getIconRes()129 public int getIconRes() { 130 return duo.getLogo(); 131 } 132 } 133