OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.gsa; |
| 6 |
| 7 import android.accounts.Account; |
| 8 import android.content.Context; |
| 9 import android.content.Intent; |
| 10 import android.content.pm.PackageManager; |
| 11 import android.content.pm.ResolveInfo; |
| 12 import android.text.TextUtils; |
| 13 |
| 14 import org.chromium.base.PackageUtils; |
| 15 import org.chromium.sync.signin.ChromeSigninController; |
| 16 |
| 17 /** |
| 18 * A class responsible fore representing the current state of Chrome's integrati
on with GSA. |
| 19 */ |
| 20 public class GSAState { |
| 21 private static final int GSA_VERSION_FOR_DOCUMENT = 300401021; |
| 22 private static final int GMS_CORE_VERSION = 6577010; |
| 23 |
| 24 static final String SEARCH_INTENT_PACKAGE = "com.google.android.googlequicks
earchbox"; |
| 25 private static final String GMS_CORE_PACKAGE = "com.google.android.gms"; |
| 26 |
| 27 static final String SEARCH_INTENT_ACTION = |
| 28 "com.google.android.googlequicksearchbox.TEXT_ASSIST"; |
| 29 |
| 30 /** |
| 31 * An instance of GSAState class encapsulating knowledge about the current s
tatus. |
| 32 */ |
| 33 private static GSAState sGSAState; |
| 34 |
| 35 /** |
| 36 * The application context to use. |
| 37 */ |
| 38 private final Context mContext; |
| 39 |
| 40 /** |
| 41 * Caches the result of a computation on whether GSA is available. |
| 42 */ |
| 43 private Boolean mGsaAvailable; |
| 44 |
| 45 /** |
| 46 * The Google account being used by GSA according to the latest update we ha
ve received. |
| 47 * This may be null. |
| 48 */ |
| 49 private String mGsaAccount; |
| 50 |
| 51 /** |
| 52 * Returns the singleton instance of GSAState and creates one if necessary. |
| 53 * @param context The context to use. |
| 54 * @return The state object. |
| 55 */ |
| 56 public static GSAState getInstance(Context context) { |
| 57 if (sGSAState == null) { |
| 58 sGSAState = new GSAState(context); |
| 59 } |
| 60 return sGSAState; |
| 61 } |
| 62 |
| 63 /* Private constructor, since this is a singleton */ |
| 64 private GSAState(Context context) { |
| 65 mContext = context.getApplicationContext(); |
| 66 } |
| 67 |
| 68 /** |
| 69 * Update the GSA logged in account name and whether we are in GSA holdback. |
| 70 * @param gsaAccount The email address of the logged in account. |
| 71 */ |
| 72 public void setGsaAccount(String gsaAccount) { |
| 73 mGsaAccount = gsaAccount; |
| 74 } |
| 75 |
| 76 /** |
| 77 * @return Whether GSA and Chrome are logged in with the same account. The s
ituation where |
| 78 * both are logged out is not considered a match. |
| 79 */ |
| 80 public boolean doesGsaAccountMatchChrome() { |
| 81 Account chromeUser = ChromeSigninController.get(mContext).getSignedInUse
r(); |
| 82 return chromeUser != null && !TextUtils.isEmpty(mGsaAccount) && TextUtil
s.equals( |
| 83 chromeUser.name, mGsaAccount); |
| 84 } |
| 85 |
| 86 /** |
| 87 * @return The current GSA account. May return null if GSA hasn't replied ye
t. |
| 88 */ |
| 89 public String getGsaAccount() { |
| 90 return mGsaAccount; |
| 91 } |
| 92 |
| 93 /** |
| 94 * This is used to check whether GSA package is available to handle search r
equests and if |
| 95 * the Chrome experiment to do so is enabled. |
| 96 * @return Whether the search intent this class creates will resolve to an a
ctivity. |
| 97 */ |
| 98 public boolean isGsaAvailable() { |
| 99 if (mGsaAvailable != null) return mGsaAvailable; |
| 100 mGsaAvailable = false; |
| 101 PackageManager pm = mContext.getPackageManager(); |
| 102 Intent searchIntent = new Intent(SEARCH_INTENT_ACTION); |
| 103 searchIntent.setPackage(GSAState.SEARCH_INTENT_PACKAGE); |
| 104 ResolveInfo resolveInfo = pm.resolveActivity(searchIntent, 0); |
| 105 if (resolveInfo == null || resolveInfo.activityInfo == null) { |
| 106 mGsaAvailable = false; |
| 107 } else if (!isPackageAboveVersion(SEARCH_INTENT_PACKAGE, GSA_VERSION_FOR
_DOCUMENT) |
| 108 || !isPackageAboveVersion(GMS_CORE_PACKAGE, GMS_CORE_VERSION)) { |
| 109 mGsaAvailable = false; |
| 110 } else { |
| 111 mGsaAvailable = true; |
| 112 } |
| 113 return mGsaAvailable; |
| 114 } |
| 115 |
| 116 /** |
| 117 * Check whether the given package meets min requirements for using full doc
ument mode. |
| 118 * @param packageName The package name we are inquiring about. |
| 119 * @param minVersion The minimum version for the package to be. |
| 120 * @return Whether the package exists on the device and its version is highe
r than the minimum |
| 121 * required version. |
| 122 */ |
| 123 private boolean isPackageAboveVersion(String packageName, int minVersion) { |
| 124 return PackageUtils.getPackageVersion(mContext, packageName) >= minVersi
on; |
| 125 } |
| 126 } |
OLD | NEW |