Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit 5ae047d

Browse files
committed
cache play services check
1 parent db7fb11 commit 5ae047d

File tree

1 file changed

+46
-42
lines changed

1 file changed

+46
-42
lines changed

app/src/main/java/com/otaupdater/utils/Utils.java

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package com.otaupdater.utils;
1818

19-
import android.app.Activity;
2019
import android.app.AlertDialog;
2120
import android.content.Context;
2221
import android.content.DialogInterface;
@@ -30,8 +29,6 @@
3029
import android.provider.Settings;
3130
import android.telephony.TelephonyManager;
3231
import android.util.Log;
33-
import android.view.View;
34-
import android.widget.Toast;
3532

3633
import com.google.android.gms.common.ConnectionResult;
3734
import com.google.android.gms.common.GooglePlayServicesUtil;
@@ -63,6 +60,8 @@ public class Utils {
6360
private static final Random random = new SecureRandom();
6461
private static final SimpleDateFormat OTA_DATE = new SimpleDateFormat("yyyyMMdd-kkmm", Locale.US);
6562

63+
private static Boolean cachedPlayServicesCheck = null;
64+
6665
private Utils() { }
6766

6867
public static int getAppVersion(Context ctx) {
@@ -139,52 +138,57 @@ public static String hmac(String str, String key) {
139138
return null;
140139
}
141140

142-
public static void toastWrapper(final Activity activity, final CharSequence text, final int duration) {
143-
activity.runOnUiThread(new Runnable() {
144-
@Override public void run() {
145-
Toast.makeText(activity, text, duration).show();
146-
}
147-
});
148-
}
149-
150-
public static void toastWrapper(final Activity activity, final int resId, final int duration) {
151-
activity.runOnUiThread(new Runnable() {
152-
@Override public void run() {
153-
Toast.makeText(activity, resId, duration).show();
154-
}
155-
});
156-
}
157-
158-
public static void toastWrapper(final View view, final CharSequence text, final int duration) {
159-
view.post(new Runnable() {
160-
@Override public void run() {
161-
Toast.makeText(view.getContext(), text, duration).show();
162-
}
163-
});
164-
}
165-
166-
public static void toastWrapper(final View view, final int resId, final int duration) {
167-
view.post(new Runnable() {
168-
@Override public void run() {
169-
Toast.makeText(view.getContext(), resId, duration).show();
170-
}
171-
});
172-
}
141+
// public static void toastWrapper(final Activity activity, final CharSequence text, final int duration) {
142+
// activity.runOnUiThread(new Runnable() {
143+
// @Override public void run() {
144+
// Toast.makeText(activity, text, duration).show();
145+
// }
146+
// });
147+
// }
148+
//
149+
// public static void toastWrapper(final Activity activity, final int resId, final int duration) {
150+
// activity.runOnUiThread(new Runnable() {
151+
// @Override public void run() {
152+
// Toast.makeText(activity, resId, duration).show();
153+
// }
154+
// });
155+
// }
156+
//
157+
// public static void toastWrapper(final View view, final CharSequence text, final int duration) {
158+
// view.post(new Runnable() {
159+
// @Override public void run() {
160+
// Toast.makeText(view.getContext(), text, duration).show();
161+
// }
162+
// });
163+
// }
164+
//
165+
// public static void toastWrapper(final View view, final int resId, final int duration) {
166+
// view.post(new Runnable() {
167+
// @Override public void run() {
168+
// Toast.makeText(view.getContext(), resId, duration).show();
169+
// }
170+
// });
171+
// }
173172

174173
public static boolean checkPlayServices(Context ctx) {
175-
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(ctx);
176-
if (resultCode != ConnectionResult.SUCCESS) {
177-
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
178-
Log.v(Config.LOG_TAG + "checkPlayServices", "Play Services error: " + GooglePlayServicesUtil.getErrorString(resultCode));
174+
if (cachedPlayServicesCheck == null) {
175+
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(ctx);
176+
if (resultCode == ConnectionResult.SUCCESS) {
177+
cachedPlayServicesCheck = true;
178+
} else {
179+
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
180+
Log.v(Config.LOG_TAG + "checkPlayServices", "Play Services error: " + GooglePlayServicesUtil.getErrorString(resultCode));
179181
// if (ctx instanceof Activity) {
180182
// GooglePlayServicesUtil.getErrorDialog(resultCode, (Activity) ctx, REQUEST_CODE_RECOVER_PLAY_SERVICES).show();
181183
// }
182-
} else {
183-
Log.v(Config.LOG_TAG + "checkPlayServices", "Device not supported");
184+
} else {
185+
Log.v(Config.LOG_TAG + "checkPlayServices", "Device not supported");
186+
}
187+
cachedPlayServicesCheck = false;
184188
}
185-
return false;
186189
}
187-
return true;
190+
191+
return cachedPlayServicesCheck;
188192
}
189193

190194
public static boolean dataAvailable(Context ctx) {

0 commit comments

Comments
 (0)