Skip to content

Commit

Permalink
Add FCM to receive weather alerts
Browse files Browse the repository at this point in the history
  • Loading branch information
Protino committed Nov 13, 2016
1 parent a4cebd7 commit a9fbf15
Show file tree
Hide file tree
Showing 35 changed files with 420 additions and 123 deletions.
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
*.iml
/.idea
/local.properties
/.idea/misc.xml
/.idea/workspace.xml
/.idea/misc.xml
/.idea/libraries
/gradle.properties
/app/google-services.json
.DS_Store
/build
/captures
1 change: 0 additions & 1 deletion .idea/.name

This file was deleted.

22 changes: 0 additions & 22 deletions .idea/compiler.xml

This file was deleted.

3 changes: 0 additions & 3 deletions .idea/copyright/profiles_settings.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/encodings.xml

This file was deleted.

18 changes: 0 additions & 18 deletions .idea/gradle.xml

This file was deleted.

9 changes: 0 additions & 9 deletions .idea/modules.xml

This file was deleted.

12 changes: 0 additions & 12 deletions .idea/runConfigurations.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
I had completed 70% of the Sunshine app almost 3 months. I've started the android developer nanodegree course in Udacity
and found they used the same app to teach. I was in a dilemma of whether to skip forward and get going with projects. But, I decided to
redo the Sunshine app from the beginning to review my knowledge.
Sunshine
========
This is an android application which shows real-time weather data by using <a href="http://openweathermap.org" target="_blank">OpenWeatherMapAPI</a>.It is part of the Udacity Android Developer Nanodegree coursework.

**Build steps**
1. Clone the repository
2. Create gradle.properties in the root folder
3. Initialize the following variable in the file

### Build configuration

1. Clone the repository.
2. Add google-services.json created by firebase console.
3. Create gradle.properties in the root folder.
4. Initialize the following variable in the file.

`MyOpenWeatherMapApiKey = "[YOUR_API_KEY_HERE]"`

You can get an API_KEY from <a href="http://openweathermap.org" target="_blank">Open Weather</a>

Please report issues with log errors if not possible, screenshots will do.
Please report issues by attaching log errors, if possible else screenshots will do.
7 changes: 7 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,11 @@ dependencies {
compile 'com.android.support:design:23.2.0'
compile 'com.android.support:support-v4:23.2.0'
compile 'com.android.support:support-annotations:23.2.0'
compile 'com.github.bumptech.glide:glide:3.5.2'
compile 'com.google.android.gms:play-services:9.8.0'
compile 'com.google.firebase:firebase-core:9.8.0'
compile 'com.google.firebase:firebase-messaging:9.8.0'
}


apply plugin: 'com.google.gms.google-services'
42 changes: 42 additions & 0 deletions app/google-services.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"project_info": {
"project_number": "29065058461",
"firebase_url": "https://sunshine-149409.firebaseio.com",
"project_id": "sunshine-149409",
"storage_bucket": "sunshine-149409.appspot.com"
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:29065058461:android:5220e06c88366bdb",
"android_client_info": {
"package_name": "com.calgen.prodek.sunshine_v2"
}
},
"oauth_client": [
{
"client_id": "29065058461-mt74mvcku8apthekiroa3v18rhkir5ku.apps.googleusercontent.com",
"client_type": 3
}
],
"api_key": [
{
"current_key": "AIzaSyCy7jH9raupaBQBCrfeZdQ0Og2MeYIOS7Y"
}
],
"services": {
"analytics_service": {
"status": 1
},
"appinvite_service": {
"status": 1,
"other_platform_oauth_client": []
},
"ads_service": {
"status": 2
}
}
}
],
"configuration_version": "1"
}
58 changes: 46 additions & 12 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.calgen.prodek.sunshine_v2">
package="com.calgen.prodek.sunshine_v2">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<!-- Permissions required for Google Cloud Messaging-->
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>


<!-- Permissions required by the sync adapter -->
<uses-permission
android:name="android.permission.READ_SYNC_SETTINGS"/>
Expand All @@ -23,9 +29,9 @@
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
Expand All @@ -34,44 +40,72 @@
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.MainActivity" />
android:value=".activity.MainActivity"/>
</activity>
<activity
android:name=".activity.SettingsActivity"
android:label="@string/title_activity_settings">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.MainActivity" />
android:value=".activity.MainActivity"/>
</activity>

<!-- SyncAdapter's dummy authentication service -->
<service
android:name=".sync.SunshineAuthenticatorService"
>
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator" />
<action android:name="android.accounts.AccountAuthenticator"/>
</intent-filter>
<meta-data
android:name="android.accounts.AccountAuthenticator"
android:resource="@xml/authenticator" />
android:resource="@xml/authenticator"/>
</service>

<provider
android:authorities="@string/content_authority"
android:name=".data.WeatherProvider"
android:authorities="@string/content_authority"
android:exported="false"
android:syncable="true" />
android:syncable="true"/>

<!-- The SyncAdapter service -->
<service
android:name=".sync.SunshineSyncService"
android:exported="true">
<intent-filter>
<action android:name="android.content.SyncAdapter" />
<action android:name="android.content.SyncAdapter"/>
</intent-filter>
<meta-data
android:name="android.content.SyncAdapter"
android:resource="@xml/syncadapter" />
android:resource="@xml/syncadapter"/>
</service>

<!-- The Firebase Cloud Messaging receiver and services -->
<service
android:name=".fcm.MyFcmListenerService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>

<service
android:name=".fcm.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>


<!-- Set custom default icon. This is used when no icon is set for incoming notification messages.
See README(https://goo.gl/l4GJaQ) for more. -->
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_priority_high_white_24dp"/>
<!-- Set color used with incoming notification messages. This is used when no color is set for the incoming
notification message. See README(https://goo.gl/6BKBk7) for more. -->
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent"/>

</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@
import com.calgen.prodek.sunshine_v2.fragment.DetailFragment;
import com.calgen.prodek.sunshine_v2.fragment.ForecastFragment;
import com.calgen.prodek.sunshine_v2.sync.SunshineSyncAdapter;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;

public class MainActivity extends AppCompatActivity implements ForecastFragment.Callback {

public static final String SENT_TOKEN_TO_SERVER = "sentTokenToServer";
private static final String TAG = MainActivity.class.getSimpleName();
private static final String DETAIL_FRAGMENT_TAG = "DFTAG";
private static final String PANE_TYPE = "pane_type";
private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
private static boolean mTwoPane;
private String mLocation;

Expand Down Expand Up @@ -49,8 +53,25 @@ protected void onCreate(Bundle savedInstanceState) {

SunshineSyncAdapter.initializeSyncAdapter(this);

// If Google Play Services is up to date, we'll want to register GCM. If it is not, we'll
// skip the registration and this device will not receive any downstream messages from
// our fake server. Because weather alerts are not a core feature of the app, this should
// not affect the behavior of the app, from a user perspective.
if (checkPlayServices()) {
// Because this is the initial creation of the app, we'll want to be certain we have
// a token. If we do not, then we will start the IntentService that will register this
// application with GCM.
SharedPreferences sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(this);
boolean sentToken = sharedPreferences.getBoolean(SENT_TOKEN_TO_SERVER, false);
if (!sentToken) {
//Intent intent = new Intent(this, RegistrationIntentService.class);
//startService(intent);
}
}
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
Expand Down Expand Up @@ -103,6 +124,13 @@ private void openPreferredLocationInMap() {
@Override
protected void onResume() {
super.onResume();

// If Google Play Services is not available, some features, such as GCM-powered weather
// alerts, will not be available.
if (!checkPlayServices()) {
// Store regID as null
}

String location = Utility.getPreferredLocation(this);
// update the location in our second pane using the fragment manager
if (location != null && !location.equals(mLocation)) {
Expand Down Expand Up @@ -136,4 +164,26 @@ public void onItemSelected(Uri dateUri) {
}
}

/**
* 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.
*
* @return true if PlayServices is available else false
*/
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(TAG, "This device is not supported");
finish();
}
return false;
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public void onCreate(Bundle savedInstanceState) {
// For all preferences, attach an OnPreferenceChangeListener so the UI summary can be
// updated when the preference changes.
bindPreferenceSummaryToValue(findPreference(getResources().getString(R.string.pref_location_key)));
bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_icon_pack_key)));
bindPreferenceSummaryToValue(findPreference(getResources().getString(R.string.pref_temperature_key)));
CheckBoxPreference notificationPreference = (CheckBoxPreference) findPreference(getResources().getString(R.string.pref_notification_key));
notificationPreference.setOnPreferenceChangeListener(this);
Expand Down
Loading

0 comments on commit a9fbf15

Please sign in to comment.