Commit 8e8a9389 authored by Sajal Narang's avatar Sajal Narang

Show prompt for updating app

parent 68a38620
...@@ -6,11 +6,14 @@ import android.app.NotificationChannel; ...@@ -6,11 +6,14 @@ import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.graphics.Color; import android.graphics.Color;
import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.support.design.widget.NavigationView; import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction; import android.support.v4.app.FragmentTransaction;
...@@ -31,8 +34,12 @@ import android.widget.Toast; ...@@ -31,8 +34,12 @@ import android.widget.Toast;
import com.google.android.gms.tasks.OnSuccessListener; import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.iid.FirebaseInstanceId; import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult; import com.google.firebase.iid.InstanceIdResult;
import com.google.gson.JsonObject;
import com.squareup.picasso.Picasso; import com.squareup.picasso.Picasso;
import org.json.JSONException;
import org.json.JSONObject;
import app.insti.Constants; import app.insti.Constants;
import app.insti.R; import app.insti.R;
import app.insti.SessionManager; import app.insti.SessionManager;
...@@ -132,9 +139,53 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On ...@@ -132,9 +139,53 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
} }
} }
checkLatestVersion();
NotificationEventReceiver.setupAlarm(getApplicationContext()); NotificationEventReceiver.setupAlarm(getApplicationContext());
} }
private void checkLatestVersion() {
try {
PackageInfo pInfo = this.getPackageManager().getPackageInfo(getPackageName(), 0);
final int versionCode = pInfo.versionCode;
RetrofitInterface retrofitInterface = ServiceGenerator.createService(RetrofitInterface.class);
retrofitInterface.getLatestVersion().enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
System.out.println("Check" + response.code() + response.body());
if (response.isSuccessful()) {
System.out.println("Check 2" + response.body());
if (response.body().get("version").getAsInt() > versionCode) {
showUpdateSnackBar();
}
}
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
}
});
} catch (PackageManager.NameNotFoundException ignored) {
}
}
private void showUpdateSnackBar() {
View parentLayout = findViewById(android.R.id.content);
Snackbar.make(parentLayout, "New Version Available", Snackbar.LENGTH_LONG).setAction("UPDATE", new View.OnClickListener() {
@Override
public void onClick(View v) {
final String appPackageName = getPackageName(); // getPackageName() from Context or Activity object
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}
}
}).show();
}
@TargetApi(Build.VERSION_CODES.O) @TargetApi(Build.VERSION_CODES.O)
private void createNotificationChannel() { private void createNotificationChannel() {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
......
package app.insti.api; package app.insti.api;
import com.google.gson.JsonObject;
import org.json.JSONObject;
import java.util.List; import java.util.List;
import app.insti.api.model.EventCreateRequest; import app.insti.api.model.EventCreateRequest;
...@@ -26,6 +30,9 @@ import retrofit2.http.Path; ...@@ -26,6 +30,9 @@ import retrofit2.http.Path;
import retrofit2.http.Query; import retrofit2.http.Query;
public interface RetrofitInterface { public interface RetrofitInterface {
@GET("../andro.json")
Call<JsonObject> getLatestVersion();
@GET("login") @GET("login")
Call<LoginResponse> login(@Query("code") String AUTH_CODE, @Query("redir") String redirectURI, @Query("fcm_id") String fcmID); Call<LoginResponse> login(@Query("code") String AUTH_CODE, @Query("redir") String redirectURI, @Query("fcm_id") String fcmID);
......
package app.insti.api; package app.insti.api;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit; import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory; import retrofit2.converter.gson.GsonConverterFactory;
...@@ -13,7 +14,9 @@ public class ServiceGenerator { ...@@ -13,7 +14,9 @@ public class ServiceGenerator {
private static Retrofit retrofit; private static Retrofit retrofit;
public static <S> S createService(Class<S> serviceClass) { public static <S> S createService(Class<S> serviceClass) {
retrofit = retrofitBuilder.client(clientBuilder.build()).build(); HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
retrofit = retrofitBuilder.client(clientBuilder.addInterceptor(loggingInterceptor).build()).build();
return retrofit.create(serviceClass); return retrofit.create(serviceClass);
} }
} }
...@@ -13,6 +13,6 @@ ...@@ -13,6 +13,6 @@
android:layout_alignParentRight="true" android:layout_alignParentRight="true"
android:layout_alignParentStart="true" android:layout_alignParentStart="true"
android:layout_alignParentTop="false" android:layout_alignParentTop="false"
android:layout_alignWithParentIfMissing="false"></WebView> android:layout_alignWithParentIfMissing="false" />
</LinearLayout> </LinearLayout>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment