1
1
package softwaremobility .darkgeat .sunshine ;
2
2
3
+ import android .content .Context ;
3
4
import android .content .Intent ;
4
5
import android .content .SharedPreferences ;
6
+ import android .content .pm .PackageInfo ;
7
+ import android .content .pm .PackageManager ;
5
8
import android .net .Uri ;
9
+ import android .os .AsyncTask ;
6
10
import android .preference .PreferenceManager ;
7
11
import android .support .v7 .app .ActionBarActivity ;
8
12
import android .os .Bundle ;
13
+ import android .support .v7 .app .AlertDialog ;
14
+ import android .util .Log ;
9
15
import android .view .Menu ;
10
16
import android .view .MenuItem ;
11
17
18
+ import com .google .android .gms .common .ConnectionResult ;
19
+ import com .google .android .gms .common .GooglePlayServicesUtil ;
20
+ import com .google .android .gms .gcm .GoogleCloudMessaging ;
21
+
22
+ import java .io .IOException ;
23
+ import java .sql .Connection ;
24
+
12
25
import softwaremobility .darkgeat .sunshine .sync .SyncAdapter ;
13
26
14
27
@@ -17,12 +30,20 @@ public class MainActivity extends ActionBarActivity implements ForecastFragment.
17
30
private String mLocation ;
18
31
private boolean mTwoPane ;
19
32
public static final String DETAILFRAGMENT_TAG = "DetailFragmentTAG" ;
33
+ public static final String LOG_TAG = MainActivity .class .getSimpleName ();
34
+ public static final String PROPERTY_REG_ID = "registration_id" ;
35
+ private static final String PROPERTY_APP_VERSION = "appVersion" ;
36
+ private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000 ;
37
+ private String SENDER_ID ;
38
+ private GoogleCloudMessaging mGcm ;
20
39
21
40
@ Override
22
41
protected void onCreate (Bundle savedInstanceState ) {
23
42
super .onCreate (savedInstanceState );
24
43
setContentView (R .layout .activity_main );
25
44
45
+ SENDER_ID = getString (R .string .projectIdNumber );
46
+
26
47
if (findViewById (R .id .detail_container ) != null ){
27
48
mTwoPane = true ;
28
49
if (savedInstanceState == null ){
@@ -34,6 +55,38 @@ protected void onCreate(Bundle savedInstanceState) {
34
55
mTwoPane = false ;
35
56
}
36
57
SyncAdapter .initializeSyncAdapter (this );
58
+ if (checkPlayServices ()) {
59
+ mGcm = GoogleCloudMessaging .getInstance (this );
60
+ String regId = getRegistrationId (this );
61
+
62
+ if (SENDER_ID .equals (getString (R .string .projectIdNumber ))) {
63
+ new AlertDialog .Builder (this )
64
+ .setTitle ("Needs Sender ID" )
65
+ .setMessage ("GCM will not function in Sunshine until you replace your Sender ID with a Sender ID from the Google Developers Console." )
66
+ .setPositiveButton (android .R .string .ok , null )
67
+ .create ().show ();
68
+ } else if (regId .isEmpty ()) {
69
+ registerInBackground (this );
70
+ }
71
+ } else {
72
+ Log .i (LOG_TAG , "No valid Google Play Services APK. Weather alerts will be disabled." );
73
+ // Store regID as null
74
+ storeRegistrationId (this , null );
75
+ }
76
+ }
77
+
78
+ private boolean checkPlayServices () {
79
+ int resultCode = GooglePlayServicesUtil .isGooglePlayServicesAvailable (this );
80
+ if (resultCode != ConnectionResult .SUCCESS ){
81
+ if (GooglePlayServicesUtil .isUserRecoverableError (resultCode )){
82
+ GooglePlayServicesUtil .getErrorDialog (resultCode ,this ,PLAY_SERVICES_RESOLUTION_REQUEST ).show ();
83
+ }else {
84
+ Log .i (LOG_TAG ,"This device is not supported." );
85
+ finish ();
86
+ }
87
+ return false ;
88
+ }
89
+ return true ;
37
90
}
38
91
39
92
@@ -105,4 +158,68 @@ public void onItemSelected(Uri dateUri) {
105
158
startActivity (intent );
106
159
}
107
160
}
161
+
162
+ private void registerInBackground (final Context context ){
163
+ new AsyncTask <Void ,Void ,Void >(){
164
+
165
+ @ Override
166
+ protected Void doInBackground (Void ... params ) {
167
+ String msg = "" ;
168
+ try {
169
+ if (mGcm == null ){
170
+ mGcm = GoogleCloudMessaging .getInstance (context );
171
+ }
172
+ String regId = mGcm .register (SENDER_ID );
173
+ msg = "Device registered, registration ID = " + regId ;
174
+
175
+ storeRegistrationId (context ,regId );
176
+ Log .d (LOG_TAG , msg );
177
+ }catch (IOException e ){}
178
+ return null ;
179
+ }
180
+ }.execute ();
181
+ }
182
+
183
+ private String getRegistrationId (Context context ){
184
+ final SharedPreferences preferences = getGCMPreferences ();
185
+ String registrationId = preferences .getString (PROPERTY_REG_ID ,"" );
186
+ if (registrationId .isEmpty ()){
187
+ Log .i (LOG_TAG ,"GCM Registration not Found" );
188
+ return "" ;
189
+ }
190
+ int registeredVersion = preferences .getInt (PROPERTY_APP_VERSION ,Integer .MIN_VALUE );
191
+ int currentVersion = getAppVersion (context );
192
+ if (registeredVersion != currentVersion ){
193
+ Log .i (LOG_TAG ,"App version changed." );
194
+ return "" ;
195
+ }
196
+ return registrationId ;
197
+ }
198
+
199
+ private void storeRegistrationId (Context context , String regId ) {
200
+ final SharedPreferences preferences = getGCMPreferences ();
201
+ int appVersion = getAppVersion (context );
202
+ Log .i (LOG_TAG , "Saving regId on app version " + appVersion );
203
+ SharedPreferences .Editor editor = preferences .edit ();
204
+ editor .putString (PROPERTY_REG_ID ,regId );
205
+ editor .putInt (PROPERTY_APP_VERSION , appVersion );
206
+ editor .commit ();
207
+ }
208
+
209
+ public static int getAppVersion (Context context ) {
210
+ try {
211
+ PackageInfo packageInfo = context .getPackageManager ().getPackageInfo (context .getPackageName (),0 );
212
+ return packageInfo .versionCode ;
213
+ } catch (PackageManager .NameNotFoundException e ) {
214
+ e .printStackTrace ();
215
+ throw new RuntimeException ("Could not get package name: " + e );
216
+ }
217
+ }
218
+
219
+ private SharedPreferences getGCMPreferences () {
220
+ // Sunshine persists the registration ID in shared preferences, but
221
+ // how you store the registration ID in your app is up to you. Just make sure
222
+ // that it is private!
223
+ return getSharedPreferences (LOG_TAG , Context .MODE_PRIVATE );
224
+ }
108
225
}
0 commit comments