/[projects]/android/CheckUpdates/src/dk/thoerup/checkupdates/CheckUpdates.java
ViewVC logotype

Annotation of /android/CheckUpdates/src/dk/thoerup/checkupdates/CheckUpdates.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 721 - (hide annotations) (download)
Mon May 10 19:45:11 2010 UTC (14 years ago) by torben
File size: 4616 byte(s)
url-encode the parameters
1 torben 719 package dk.thoerup.checkupdates;
2    
3     import java.io.ByteArrayOutputStream;
4     import java.io.IOException;
5     import java.io.InputStream;
6     import java.net.URL;
7     import java.net.URLConnection;
8    
9     import android.app.AlertDialog;
10     import android.content.Context;
11     import android.content.DialogInterface;
12     import android.content.Intent;
13     import android.content.SharedPreferences;
14     import android.content.DialogInterface.OnClickListener;
15     import android.content.SharedPreferences.Editor;
16     import android.content.pm.PackageInfo;
17     import android.content.pm.PackageManager;
18     import android.content.pm.PackageManager.NameNotFoundException;
19     import android.net.Uri;
20     import android.os.AsyncTask;
21     import android.util.Log;
22    
23     public class CheckUpdates {
24     public static long TIMESPAN_DAY = 24*60*60*1000; // one day
25     final static String PREFS = "CheckUpdate";
26    
27     int versionCode;
28     String versionName;
29     String packageName;
30     String phone_model;
31     String androidVersion;
32    
33     String title;
34    
35    
36     Context context;
37    
38     public void checkForUpdates(Context context, String url, String title) {
39     this.context = context;
40     this.title = title;
41    
42     SharedPreferences prefs = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE);
43     long lastCheck = prefs.getLong("lastcheck", 0);
44    
45     long now = System.currentTimeMillis(); //should i use android.os.SystemClock.elapsedRealtime() instead ?
46    
47     if ( (now+TIMESPAN_DAY) > lastCheck ){
48    
49     PackageManager pm = context.getPackageManager();
50     try {
51     PackageInfo pi;
52    
53     pi = pm.getPackageInfo(context.getPackageName(), 0);
54    
55     versionCode = pi.versionCode;
56     versionName = pi.versionName;
57     packageName = pi.packageName;
58     phone_model = android.os.Build.MODEL;
59     androidVersion = android.os.Build.VERSION.RELEASE;
60    
61    
62     UpdateTask task = new UpdateTask();
63     task.execute(url);
64    
65    
66    
67     } catch (NameNotFoundException e) {
68     e.printStackTrace();
69     }
70    
71     }
72     }
73    
74     class UpdateTask extends AsyncTask<String,Void,Void> {
75     boolean result = false;
76     int newestVersion = 0;
77    
78     @Override
79 torben 721 protected Void doInBackground(String... arg0) {
80     String requestUrl = arg0[0] + "?version=" + encode(versionName) + "&phone=" + encode(phone_model) + "&android=" + encode(androidVersion);
81 torben 719 try {
82     URL url = new URL(requestUrl);
83     URLConnection conn = url.openConnection();
84     conn.setConnectTimeout(5);
85    
86     String resultStr = readIOStream( conn.getInputStream() );
87     resultStr = resultStr.trim();
88     newestVersion = Integer.parseInt(resultStr);
89    
90     result = true;
91    
92     } catch (Exception e) {
93     Log.e("CheckUpdates", "Check for " + packageName + " failed!", e);
94     }
95    
96     return null;
97     }
98    
99 torben 721 private String encode(String data) {
100     try {
101     return java.net.URLEncoder.encode(data, "UTF-8");
102     } catch (Exception e) {
103     return data; //if encoding fails, return original and hope all goes well
104     }
105     }
106    
107 torben 719 private String readIOStream(InputStream in) throws IOException {
108     ByteArrayOutputStream baos = new ByteArrayOutputStream();
109    
110     byte buf[] = new byte[1024];
111     int len;
112    
113     while ( (len=in.read(buf)) != -1) {
114     baos.write(buf, 0, len);
115     }
116    
117     return baos.toString();
118     }
119    
120     @Override
121     protected void onPostExecute(Void r) {
122     super.onPostExecute(r);
123    
124     Log.i("CheckUpdates", "result=" + result + ", newestVersion" + newestVersion + ", versionCode=" + versionCode);
125    
126     if (result == true) {
127     if (newestVersion > versionCode) {
128     AlertDialog.Builder builder = new AlertDialog.Builder(context);
129     builder.setTitle(title);
130     builder.setMessage("This application has an a newer version available. Update now?");
131    
132     builder.setPositiveButton("Yes", new OnClickListener() {
133     public void onClick(DialogInterface dialog, int which) {
134     Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=pname:" + packageName) );
135     try {
136     context.startActivity(i);
137     } catch (Exception e) {
138     Log.e("CheckUpdates", "Activity launch failed", e);
139     }
140     context = null;
141    
142     }
143     });
144     builder.setNegativeButton("No", new OnClickListener() {
145     public void onClick(DialogInterface dialog, int which) {
146     context = null;
147     }
148     });
149    
150     try {
151     builder.show();
152     } catch (Exception e) {
153     Log.e("CheckUpdates", "Builder.show failed", e);
154     }
155    
156    
157     }
158    
159     long now = System.currentTimeMillis();
160     SharedPreferences prefs = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE);
161     //when done write
162     Editor edit = prefs.edit();
163     edit.putLong("lastCheck", now);
164     edit.commit();
165     }
166    
167     }
168    
169     }
170    
171    
172     }

  ViewVC Help
Powered by ViewVC 1.1.20