-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathForceUpdate.java
153 lines (119 loc) · 4.89 KB
/
ForceUpdate.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.GradientDrawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.IOException;
public class ForceUpdate {
private Context context;
private double versionName= Double.valueOf(BuildConfig.VERSION_NAME);
private double playStoreVersionName;
private boolean cancelable = false;
private boolean cancelableTouchOutSide = false;
private CharSequence titleText = "Update Available";
private String messageText = "An Update is Available With New Features";
public ForceUpdate(Context context){
this.context = context;
}
public void setCancelable(boolean cancelable) {
this.cancelable = cancelable;
}
public void setCanceledOnTouchOutside(boolean cancelableTouchOutSide ) {
this.cancelableTouchOutSide = cancelableTouchOutSide;
}
public void setTitle(@Nullable CharSequence title) {
this.titleText = title;
}
public void setMessage(String message) {
this.messageText = message;
}
public void build(){
if (!isConnectivityOk()){
return;
}
LoadUrl loadUrl = new LoadUrl();
loadUrl.execute();
}
@SuppressLint("StaticFieldLeak")
private class LoadUrl extends AsyncTask<Void,Void,Void> {
@Override
protected Void doInBackground(Void... voids) {
try {
String url1 = "https://play.google.com/store/apps/details?id="+context.getPackageName();
Document doc = Jsoup.connect(url1)
.userAgent("Mozilla/4.0")
.referrer("https://www.google.com")
.timeout(30000)
.get();
String ver = doc.select("div[itemprop=softwareVersion]").text();
playStoreVersionName = Double.valueOf(ver);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if (playStoreVersionName>versionName){
showUpdateDialog();
}
}
}
private void showUpdateDialog() {
final Dialog updateDialog = new Dialog(context);
updateDialog.setContentView(R.layout.update_dialog);
updateDialog.setCanceledOnTouchOutside(cancelableTouchOutSide);
TextView title = updateDialog.findViewById(R.id.title);
TextView message = updateDialog.findViewById(R.id.message);
ImageView updateIcon = updateDialog.findViewById(R.id.update_icon);
updateIcon.setImageResource(android.R.drawable.ic_dialog_info);
title.setText(titleText);
message.setText(messageText);
GradientDrawable updateShape = new GradientDrawable();
updateShape.setColor(context.getResources().getColor(android.R.color.holo_green_dark));
updateShape.setCornerRadii(new float[]{10f,10f,10f,10f,10f,10f,10f,10f});
Button updateButton = updateDialog.findViewById(R.id.updateButton);
updateButton.setBackground(updateShape);
GradientDrawable ignoreShape = new GradientDrawable();
ignoreShape.setColor(context.getResources().getColor(android.R.color.holo_red_dark));
ignoreShape.setCornerRadii(new float[]{10f,10f,10f,10f,10f,10f,10f,10f});
Button ignoreButton = updateDialog.findViewById(R.id.ignoreButton);
ignoreButton.setBackground(ignoreShape);
updateDialog.show();
updateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://play.google.com/store/apps/details?id="+context.getPackageName()));
context.startActivity(intent);
}
});
ignoreButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
updateDialog.dismiss();
}
});
}
private boolean isConnectivityOk() {
try {
NetworkInfo activeNetwork = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (activeNetwork == null || (activeNetwork.getType() != 1 && activeNetwork.getType() != 0)) {
return false;
}
return true;
} catch (Exception e) {
return false;
}
}
}