Permalink
Browse files

change MainActivity

  • Loading branch information...
1 parent df98e63 commit a77d170db604b6607781392e6576f63c3db7cf40 @susemi99 committed Jan 26, 2016
@@ -1,11 +1,27 @@
package kr.susemi99.gcm;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
import android.os.Bundle;
+import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
+import android.util.Log;
+import android.widget.TextView;
+
+import com.google.android.gms.common.ConnectionResult;
+import com.google.android.gms.common.GoogleApiAvailability;
+
+import kr.susemi99.gcm.managers.PreferenceManager;
public class MainActivity extends AppCompatActivity
{
+ private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
+
+ private TextView textHello;
+
@Override
protected void onCreate(Bundle savedInstanceState)
{
@@ -14,6 +30,62 @@ protected void onCreate(Bundle savedInstanceState)
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
+ textHello = (TextView) findViewById(R.id.text_hello);
+ }
+ @Override
+ protected void onResume()
+ {
+ super.onResume();
+
+ IntentFilter filter = new IntentFilter();
+ filter.addAction(getString(R.string.action_registration_complete));
+ LocalBroadcastManager.getInstance(this).registerReceiver(receiver, filter);
}
+
+ @Override
+ protected void onPause()
+ {
+ super.onPause();
+ LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
+ }
+
+ /**
+ * Check the device to make sure it has the Google Play Services APK. If
+ * it doesn't, display a dialog that allows users to download the APK from
+ * the Google Play Store or enable it in the device's system settings.
+ */
+ private boolean checkPlayServices()
+ {
+ GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
+ int resultCode = apiAvailability.isGooglePlayServicesAvailable(this);
+ if (resultCode != ConnectionResult.SUCCESS)
+ {
+ if (apiAvailability.isUserResolvableError(resultCode))
+ {
+ apiAvailability.getErrorDialog(this, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST).show();
+ }
+ else
+ {
+ Log.i("MainActivity | checkPlayServices", "This device is not supported.|");
+ finish();
+ }
+ return false;
+ }
+ return true;
+ }
+
+ ////////////////////////////////////////
+ // broadcast receiver
+ ///////////////////////////////////////
+ private BroadcastReceiver receiver = new BroadcastReceiver()
+ {
+ @Override
+ public void onReceive(Context context, Intent intent)
+ {
+ boolean sentToken = PreferenceManager.instance(getApplicationContext()).sentToken();
+ textHello.append("\n");
+ textHello.append(sentToken ? getString(R.string.gcm_send_message) : getString(R.string.token_error_message));
+ }
+ };
}
@@ -0,0 +1,139 @@
+package kr.susemi99.gcm.managers;
+
+import android.content.Context;
+import android.content.SharedPreferences;
+import android.preference.PreferenceManager;
+
+public class BasePreferenceManager
+{
+ protected static SharedPreferences preference;
+
+
+ protected BasePreferenceManager(Context context)
+ {
+ super();
+ preference = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
+ }
+
+
+ /**
+ * key 수동 설정
+ *
+ * @param key 키 값
+ * @param value 내용
+ */
+ protected static void put(String key, String value)
+ {
+ SharedPreferences p = preference;
+ SharedPreferences.Editor editor = p.edit();
+ editor.putString(key, value);
+ editor.commit();
+ }
+
+
+ /**
+ * String 값 가져오기
+ *
+ * @param key 키 값
+ * @return String (기본값 null)
+ */
+ protected static String get(String key)
+ {
+ return preference.getString(key, null);
+ }
+
+
+ /**
+ * String 값 가져오기
+ *
+ * @param key 키 값
+ * @return String (기본값 "")
+ */
+ protected static String getWithNullToBlank(String key)
+ {
+ return preference.getString(key, "");
+ }
+
+
+ /**
+ * key 설정
+ *
+ * @param key 키 값
+ * @param value 내용
+ */
+ protected static void put(String key, boolean value)
+ {
+ SharedPreferences p = preference;
+ SharedPreferences.Editor editor = p.edit();
+ editor.putBoolean(key, value);
+ editor.commit();
+ }
+
+
+ /**
+ * Boolean 값 가져오기
+ *
+ * @param key 키 값
+ * @param defaultValue 기본값
+ * @return Boolean
+ */
+ protected static boolean get(String key, boolean defaultValue)
+ {
+ return preference.getBoolean(key, defaultValue);
+ }
+
+
+ /**
+ * key 설정
+ *
+ * @param key 키 값
+ * @param value 내용
+ */
+ protected static void put(String key, int value)
+ {
+ SharedPreferences p = preference;
+ SharedPreferences.Editor editor = p.edit();
+ editor.putInt(key, value);
+ editor.commit();
+ }
+
+
+ /**
+ * int 값 가져오기
+ *
+ * @param key 키 값
+ * @param defaultValue 기본값
+ * @return int
+ */
+ protected static int get(String key, int defaultValue)
+ {
+ return preference.getInt(key, defaultValue);
+ }
+
+ /**
+ * key 설정
+ *
+ * @param key 키 값
+ * @param value 내용
+ */
+ protected static void put(String key, long value)
+ {
+ SharedPreferences p = preference;
+ SharedPreferences.Editor editor = p.edit();
+ editor.putLong(key, value);
+ editor.commit();
+ }
+
+
+ /**
+ * int 값 가져오기
+ *
+ * @param key 키 값
+ * @param defaultValue 기본값
+ * @return int
+ */
+ protected static long get(String key, long defaultValue)
+ {
+ return preference.getLong(key, defaultValue);
+ }
+}
@@ -0,0 +1,44 @@
+package kr.susemi99.gcm.managers;
+
+import android.content.Context;
+
+public class PreferenceManager extends BasePreferenceManager
+{
+ private static PreferenceManager instance;
+ private Context context;
+
+
+ public static PreferenceManager instance(Context context)
+ {
+ if (instance == null)
+ {
+ synchronized (PreferenceManager.class)
+ {
+ if (instance == null)
+ {
+ instance = new PreferenceManager(context);
+ }
+ }
+ }
+ return instance;
+ }
+
+
+ private PreferenceManager(Context context)
+ {
+ super(context);
+ this.context = context;
+ }
+
+ private static final String SENT_TOKEN_TO_SERVER = "sent_token_to_server";
+
+ public void sentToken(boolean sent)
+ {
+ put(SENT_TOKEN_TO_SERVER, sent);
+ }
+
+ public boolean sentToken()
+ {
+ return get(SENT_TOKEN_TO_SERVER, false);
+ }
+}
@@ -14,6 +14,7 @@
tools:showIn="@layout/activity_main">
<TextView
+ android:id="@+id/text_hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"/>
@@ -1,9 +0,0 @@
-<resources>>
-
- <style name="AppTheme.NoActionBar">
- <item name="windowActionBar">false</item>
- <item name="windowNoTitle">true</item>
- <item name="android:windowDrawsSystemBarBackgrounds">true</item>
- <item name="android:statusBarColor">@android:color/transparent</item>
- </style>
-</resources>
@@ -1,6 +0,0 @@
-<resources>
- <!-- Example customization of dimensions originally defined in res/values/dimens.xml
- (such as screen margins) for screens with more than 820dp of available width. This
- would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
- <dimen name="activity_horizontal_margin">64dp</dimen>
-</resources>
@@ -1,4 +1,13 @@
<resources>
<string name="app_name">GCM</string>
<string name="action_settings">Settings</string>
+
+ <string name="action_registration_complete">kr.susemi99.gcm.action_registration_complete</string>
+
+ <string name="gcm_send_message">Token retrieved and sent to server! You can now use gcmsender to
+ send downstream messages to this app.</string>
+ <string name="registering_message">Generating InstanceID token...</string>
+ <string name="token_error_message">An error occurred while either fetching the InstanceID token,
+ sending the fetched token to the server or subscribing to the PubSub topic. Please try
+ running the sample again.</string>
</resources>

0 comments on commit a77d170

Please sign in to comment.