/[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 719 - (hide annotations) (download)
Mon May 10 19:35:14 2010 UTC (14 years ago) by torben
File size: 4371 byte(s)
First version
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     protected Void doInBackground(String... arg0) {
80     String requestUrl = arg0[0] + "?version=" + versionName + "&phone=" + phone_model + "&android=" + androidVersion;
81     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     private String readIOStream(InputStream in) throws IOException {
100     ByteArrayOutputStream baos = new ByteArrayOutputStream();
101    
102     byte buf[] = new byte[1024];
103     int len;
104    
105     while ( (len=in.read(buf)) != -1) {
106     baos.write(buf, 0, len);
107     }
108    
109     return baos.toString();
110     }
111    
112     @Override
113     protected void onPostExecute(Void r) {
114     super.onPostExecute(r);
115    
116     Log.i("CheckUpdates", "result=" + result + ", newestVersion" + newestVersion + ", versionCode=" + versionCode);
117    
118     if (result == true) {
119     if (newestVersion > versionCode) {
120     AlertDialog.Builder builder = new AlertDialog.Builder(context);
121     builder.setTitle(title);
122     builder.setMessage("This application has an a newer version available. Update now?");
123    
124     builder.setPositiveButton("Yes", new OnClickListener() {
125     public void onClick(DialogInterface dialog, int which) {
126     Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=pname:" + packageName) );
127     try {
128     context.startActivity(i);
129     } catch (Exception e) {
130     Log.e("CheckUpdates", "Activity launch failed", e);
131     }
132     context = null;
133    
134     }
135     });
136     builder.setNegativeButton("No", new OnClickListener() {
137     public void onClick(DialogInterface dialog, int which) {
138     context = null;
139     }
140     });
141    
142     try {
143     builder.show();
144     } catch (Exception e) {
145     Log.e("CheckUpdates", "Builder.show failed", e);
146     }
147    
148    
149     }
150    
151     long now = System.currentTimeMillis();
152     SharedPreferences prefs = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE);
153     //when done write
154     Editor edit = prefs.edit();
155     edit.putLong("lastCheck", now);
156     edit.commit();
157     }
158    
159     }
160    
161     }
162    
163    
164     }

  ViewVC Help
Powered by ViewVC 1.1.20