Skip to content

Instantly share code, notes, and snippets.

@jandrop
Created May 30, 2016 10:09
Show Gist options
  • Save jandrop/f3eb111d6b5236a017f0d8e53f38fe13 to your computer and use it in GitHub Desktop.
Save jandrop/f3eb111d6b5236a017f0d8e53f38fe13 to your computer and use it in GitHub Desktop.
PlayStore checker
/*
* Copyright [2016] [Alejandro Platas Mallo]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package es.dixitalinteractive.rallyenaron.utils;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* @author Alejandro Platas Mallo
* @version 1.01
* @since 18/5/15
*/
public class CheckUpdate extends AsyncTask<String, Void, String> {
private Boolean found;
private Activity activity;
private String versionName;
public CheckUpdate(Activity activity) {
this.activity = activity;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
try {
versionName = activity.getPackageManager()
.getPackageInfo(activity.getPackageName(), 0).versionName;
versionName = versionName.replace(".", "");
versionName = versionName.trim();
Log.i("Version name: ", versionName);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
@Override
protected String doInBackground(String... params) {
String result;
try {
String response = readStream(params[0]);
result = processResponse(response);
return result;
} catch (Exception e) {
return null;
}
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (result == null) {
Toast.makeText(activity, "Error buscando actualizaciones", Toast.LENGTH_SHORT).show();
}
if (result != null) {
Log.i("ONLINE VERSION", result);
Log.i("DEVICE VERSION", versionName);
if (Integer.valueOf(result) > Integer.valueOf(versionName) && found) {
if (!activity.isFinishing()) {
createAlertDialog();
}
}
}
}
private void createAlertDialog() {
AlertDialog.Builder bld = new AlertDialog.Builder(activity);
bld.setTitle("Actualizar");
bld.setMessage("Se ha encontrado una nueva versión de la aplicación en Google Play. ¿Desea actualizar?");
bld.setPositiveButton("Actualizar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri
.parse("market://details?id=es.dixitalinteractive.rallyenaron")));
}
});
bld.setNegativeButton("No", null);
bld.create().show();
}
private String readStream(String s) {
StringBuilder result = new StringBuilder();
HttpURLConnection urlConnection = null;
try {
URL url = new URL(s);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
return result.toString();
}
@Nullable
private String processResponse(String response) {
String result;// <div class="content" itemprop="softwareVersion"> 2.0 </div>
String searchString = "itemprop=\"softwareVersion\">";
// Return true if exists the search string
found = response.contains(searchString);
if (!found) {
return null;
} else {
// temp Store a string
// itemprop="softwareversion.......
String temp = response.substring(response.indexOf(searchString));
result = response.substring(response.indexOf(searchString), response.indexOf(searchString)
+ temp.indexOf("<"));
result = result.substring(result.indexOf(">") + 2);
result = result.replace(".", "");
result = result.trim();
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment