OSDN Git Service

d301099ec1307442ab6b4a9ffb601e0eb28fa609
[android-x86/packages-apps-Settings.git] / src / com / android / settings / support / NewDeviceIntroSuggestionActivity.java
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.settings.support;
18
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;
30
31 import com.android.settings.R;
32 import com.android.settings.dashboard.suggestions.SuggestionFeatureProvider;
33 import com.android.settings.overlay.FeatureFactory;
34
35 import java.util.List;
36
37 public class NewDeviceIntroSuggestionActivity extends Activity {
38
39     private static final String TAG = "NewDeviceIntroSugg";
40     @VisibleForTesting
41     static final String PREF_KEY_SUGGGESTION_FIRST_DISPLAY_TIME =
42             "pref_new_device_intro_suggestion_first_display_time_ms";
43     @VisibleForTesting
44     static final String PREF_KEY_SUGGGESTION_COMPLETE =
45             "pref_new_device_intro_suggestion_complete";
46     @VisibleForTesting
47     static final long PERMANENT_DISMISS_THRESHOLD = DateUtils.DAY_IN_MILLIS * 14;
48
49     @Override
50     protected void onCreate(Bundle savedInstanceState) {
51         super.onCreate(savedInstanceState);
52         final Intent intent = getLaunchIntent(this);
53         if (intent != null) {
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);
59         }
60         finish();
61     }
62
63     public static boolean isSuggestionComplete(Context context) {
64         return isExpired(context) || hasLaunchedBefore(context) || !canOpenUrlInBrowser(context);
65     }
66
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;
73
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();
77         } else {
78             firstDisplayTimeMs = prefs.getLong(PREF_KEY_SUGGGESTION_FIRST_DISPLAY_TIME, -1);
79         }
80
81         final long dismissTimeMs = firstDisplayTimeMs + PERMANENT_DISMISS_THRESHOLD;
82
83         final boolean expired = currentTimeMs > dismissTimeMs;
84
85         Log.d(TAG, "is suggestion expired: " + expired);
86         return expired;
87     }
88
89     private static boolean canOpenUrlInBrowser(Context context) {
90         final Intent intent = getLaunchIntent(context);
91         if (intent == null) {
92             // No url/intent to launch.
93             return false;
94         }
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;
99     }
100
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);
106     }
107
108     @VisibleForTesting
109     static Intent getLaunchIntent(Context context) {
110         final String url = context.getString(R.string.new_device_suggestion_intro_url);
111         if (TextUtils.isEmpty(url)) {
112             return null;
113         }
114         return new Intent()
115                 .setAction(Intent.ACTION_VIEW)
116                 .addCategory(Intent.CATEGORY_BROWSABLE)
117                 .setData(Uri.parse(url));
118     }
119 }