/[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 722 - (hide annotations) (download)
Mon May 10 19:55:13 2010 UTC (14 years ago) by torben
File size: 4651 byte(s)
Use a fixed string for log and preferences
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 torben 722 final static String CHECKUPDATES = "CheckUpdates";
26 torben 719
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 torben 722 SharedPreferences prefs = context.getSharedPreferences(CHECKUPDATES, Context.MODE_PRIVATE);
43 torben 719 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     } catch (NameNotFoundException e) {
67 torben 722 Log.e(CHECKUPDATES, "NamingException", e);
68 torben 719 }
69    
70     }
71     }
72    
73     class UpdateTask extends AsyncTask<String,Void,Void> {
74     boolean result = false;
75     int newestVersion = 0;
76    
77     @Override
78 torben 721 protected Void doInBackground(String... arg0) {
79     String requestUrl = arg0[0] + "?version=" + encode(versionName) + "&phone=" + encode(phone_model) + "&android=" + encode(androidVersion);
80 torben 719 try {
81     URL url = new URL(requestUrl);
82     URLConnection conn = url.openConnection();
83     conn.setConnectTimeout(5);
84    
85     String resultStr = readIOStream( conn.getInputStream() );
86     resultStr = resultStr.trim();
87     newestVersion = Integer.parseInt(resultStr);
88    
89     result = true;
90    
91     } catch (Exception e) {
92 torben 722 Log.e(CHECKUPDATES, "Check for " + packageName + " failed!", e);
93 torben 719 }
94    
95     return null;
96     }
97    
98 torben 721 private String encode(String data) {
99     try {
100     return java.net.URLEncoder.encode(data, "UTF-8");
101     } catch (Exception e) {
102     return data; //if encoding fails, return original and hope all goes well
103     }
104     }
105    
106 torben 719 private String readIOStream(InputStream in) throws IOException {
107     ByteArrayOutputStream baos = new ByteArrayOutputStream();
108    
109     byte buf[] = new byte[1024];
110     int len;
111    
112     while ( (len=in.read(buf)) != -1) {
113     baos.write(buf, 0, len);
114     }
115    
116     return baos.toString();
117     }
118    
119     @Override
120     protected void onPostExecute(Void r) {
121     super.onPostExecute(r);
122    
123 torben 722 Log.i(CHECKUPDATES, "result=" + result + ", newestVersion" + newestVersion + ", versionCode=" + versionCode);
124 torben 719
125     if (result == true) {
126     if (newestVersion > versionCode) {
127     AlertDialog.Builder builder = new AlertDialog.Builder(context);
128     builder.setTitle(title);
129     builder.setMessage("This application has an a newer version available. Update now?");
130    
131     builder.setPositiveButton("Yes", new OnClickListener() {
132     public void onClick(DialogInterface dialog, int which) {
133     Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=pname:" + packageName) );
134     try {
135     context.startActivity(i);
136     } catch (Exception e) {
137 torben 722 Log.e(CHECKUPDATES, "Activity launch failed", e);
138 torben 719 }
139     context = null;
140    
141     }
142     });
143     builder.setNegativeButton("No", new OnClickListener() {
144     public void onClick(DialogInterface dialog, int which) {
145     context = null;
146     }
147     });
148    
149     try {
150     builder.show();
151     } catch (Exception e) {
152 torben 722 Log.e(CHECKUPDATES, "Builder.show failed", e);
153 torben 719 }
154    
155    
156     }
157    
158     long now = System.currentTimeMillis();
159 torben 722 SharedPreferences prefs = context.getSharedPreferences(CHECKUPDATES, Context.MODE_PRIVATE);
160 torben 719 //when done write
161     Editor edit = prefs.edit();
162     edit.putLong("lastCheck", now);
163     edit.commit();
164     }
165    
166     }
167    
168     }
169    
170    
171     }

  ViewVC Help
Powered by ViewVC 1.1.20