2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.support;
19 import android.app.Activity;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.SharedPreferences;
23 import android.content.pm.ResolveInfo;
24 import android.net.Uri;
25 import android.os.Bundle;
26 import android.support.annotation.VisibleForTesting;
27 import android.text.TextUtils;
28 import android.text.format.DateUtils;
29 import android.util.Log;
31 import com.android.settings.R;
32 import com.android.settings.dashboard.suggestions.SuggestionFeatureProvider;
33 import com.android.settings.overlay.FeatureFactory;
35 import java.util.List;
37 public class NewDeviceIntroSuggestionActivity extends Activity {
39 private static final String TAG = "NewDeviceIntroSugg";
41 static final String PREF_KEY_SUGGGESTION_FIRST_DISPLAY_TIME =
42 "pref_new_device_intro_suggestion_first_display_time_ms";
44 static final String PREF_KEY_SUGGGESTION_COMPLETE =
45 "pref_new_device_intro_suggestion_complete";
47 static final long PERMANENT_DISMISS_THRESHOLD = DateUtils.DAY_IN_MILLIS * 14;
50 protected void onCreate(Bundle savedInstanceState) {
51 super.onCreate(savedInstanceState);
52 final Intent intent = getLaunchIntent(this);
54 final SuggestionFeatureProvider featureProvider = FeatureFactory.getFactory(this)
55 .getSuggestionFeatureProvider(this);
56 final SharedPreferences prefs = featureProvider.getSharedPrefs(this);
57 prefs.edit().putBoolean(PREF_KEY_SUGGGESTION_COMPLETE, true).commit();
58 startActivity(intent);
63 public static boolean isSuggestionComplete(Context context) {
64 return isExpired(context) || hasLaunchedBefore(context) || !canOpenUrlInBrowser(context);
67 private static boolean isExpired(Context context) {
68 final SuggestionFeatureProvider featureProvider = FeatureFactory.getFactory(context)
69 .getSuggestionFeatureProvider(context);
70 final SharedPreferences prefs = featureProvider.getSharedPrefs(context);
71 final long currentTimeMs = System.currentTimeMillis();
72 final long firstDisplayTimeMs;
74 if (!prefs.contains(PREF_KEY_SUGGGESTION_FIRST_DISPLAY_TIME)) {
75 firstDisplayTimeMs = currentTimeMs;
76 prefs.edit().putLong(PREF_KEY_SUGGGESTION_FIRST_DISPLAY_TIME, currentTimeMs).commit();
78 firstDisplayTimeMs = prefs.getLong(PREF_KEY_SUGGGESTION_FIRST_DISPLAY_TIME, -1);
81 final long dismissTimeMs = firstDisplayTimeMs + PERMANENT_DISMISS_THRESHOLD;
83 final boolean expired = currentTimeMs > dismissTimeMs;
85 Log.d(TAG, "is suggestion expired: " + expired);
89 private static boolean canOpenUrlInBrowser(Context context) {
90 final Intent intent = getLaunchIntent(context);
92 // No url/intent to launch.
95 // Make sure we can handle the intent.
96 final List<ResolveInfo> resolveInfos =
97 context.getPackageManager().queryIntentActivities(intent, 0);
98 return resolveInfos != null && resolveInfos.size() != 0;
101 private static boolean hasLaunchedBefore(Context context) {
102 final SuggestionFeatureProvider featureProvider = FeatureFactory.getFactory(context)
103 .getSuggestionFeatureProvider(context);
104 final SharedPreferences prefs = featureProvider.getSharedPrefs(context);
105 return prefs.getBoolean(PREF_KEY_SUGGGESTION_COMPLETE, false);
109 static Intent getLaunchIntent(Context context) {
110 final String url = context.getString(R.string.new_device_suggestion_intro_url);
111 if (TextUtils.isEmpty(url)) {
115 .setAction(Intent.ACTION_VIEW)
116 .addCategory(Intent.CATEGORY_BROWSABLE)
117 .setData(Uri.parse(url));