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

  ViewVC Help
Powered by ViewVC 1.1.20