Skip to content

Commit

Permalink
Send data from device and recieve in wear
Browse files Browse the repository at this point in the history
Use google werable data api and send data using service and listen for changes in wear module\n by adding an intent filter to the service.
  • Loading branch information
Protino committed Feb 27, 2017
1 parent dd0f814 commit d99a4f4
Show file tree
Hide file tree
Showing 10 changed files with 326 additions and 83 deletions.
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ android {
}
}

ext{
ext {
supportLibraryVersion = "25.0.1"
firebaseLibraryVersion = "9.8.0"
playServicesVersion = "9.8.0"
Expand All @@ -51,6 +51,7 @@ dependencies {
compile "com.google.firebase:firebase-messaging:${firebaseLibraryVersion}"
compile "com.google.android.gms:play-services-places:${playServicesVersion}"
compile "com.google.android.gms:play-services-gcm:${playServicesVersion}"
compile "com.google.android.gms:play-services-wearable:${playServicesVersion}"
}


Expand Down
73 changes: 39 additions & 34 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,18 @@
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<!-- Permissions required for Google Cloud Messaging-->
<!-- 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"/>
<uses-permission
android:name="android.permission.WRITE_SYNC_SETTINGS"/>
<uses-permission
android:name="android.permission.AUTHENTICATE_ACCOUNTS"/>
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS"/>
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"/>

<!-- Permissions required to access location -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<!-- Wear app permission -->
<uses-permission android:name="PROVICE"/>

Expand All @@ -35,7 +31,7 @@
android:value="@string/GEO_API_KEY"/>

<activity
android:name="io.github.protino.activity.MainActivity"
android:name=".activity.MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.Main">
<intent-filter>
Expand All @@ -45,15 +41,15 @@
</intent-filter>
</activity>
<activity
android:name="io.github.protino.activity.DetailActivity"
android:name=".activity.DetailActivity"
android:label="@string/title_activity_detail"
android:theme="@style/AppTheme.Details">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="io.github.protino.activity.MainActivity"/>
</activity>
<activity
android:name="io.github.protino.activity.SettingsActivity"
android:name=".activity.SettingsActivity"
android:label="@string/title_activity_settings"
android:theme="@style/SettingsTheme">
<meta-data
Expand All @@ -62,109 +58,118 @@
</activity>

<!-- SyncAdapter's dummy authentication service -->
<service
android:name="io.github.protino.sync.SunshineAuthenticatorService"
>
<service android:name=".sync.SunshineAuthenticatorService">
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator"/>
</intent-filter>

<meta-data
android:name="android.accounts.AccountAuthenticator"
android:resource="@xml/authenticator"/>
</service>

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

<!-- The SyncAdapter service -->
<service
android:name="io.github.protino.sync.SunshineSyncService"
android:name=".sync.SunshineSyncService"
android:exported="true">
<intent-filter>
<action android:name="android.content.SyncAdapter"/>
</intent-filter>

<meta-data
android:name="android.content.SyncAdapter"
android:resource="@xml/syncadapter"/>
</service>

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

<service
android:name="io.github.protino.fcm.MyFirebaseInstanceIdService">
<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. -->
<!--
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. -->
<!--
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"/>

<!--Today Widget receiver and service-->
<!-- Today Widget receiver and service -->
<receiver
android:name="io.github.protino.widget.TodayWidgetProvider"
android:name=".widget.TodayWidgetProvider"
android:label="@string/title_today_widget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
<action android:name="io.github.protino.ACTION_DATA_UPDATED"/>
</intent-filter>

<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_info_today"/>
</receiver>
<service android:name="io.github.protino.widget.TodayWidgetIntentService"/>

<!-- Detail Widget receiver and service-->
<service android:name=".widget.TodayWidgetIntentService"/>

<!-- Detail Widget receiver and service -->
<receiver
android:name="io.github.protino.widget.DetailWidgetProvider"
android:name=".widget.DetailWidgetProvider"
android:enabled="@bool/widget_detail_enabled"
android:label="@string/title_detail_widget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
<action android:name="io.github.protino.ACTION_DATA_UPDATED"/>
</intent-filter>

<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_info_detail"/>
</receiver>

<service
android:name="io.github.protino.widget.DetailWidgetRemoteViewsService"
android:name=".widget.DetailWidgetRemoteViewsService"
android:enabled="@bool/widget_detail_enabled"
android:exported="false"
android:permission="android.permission.BIND_REMOTEVIEWS"/>

<!-- Muzei Extension -->
<service
android:name="io.github.protino.muzei.WeatherMuzeiSource"
android:name=".muzei.WeatherMuzeiSource"
android:description="@string/muzei_description"
android:icon="@mipmap/ic_muzei"
android:label="@string/app_name">
<intent-filter>
<action android:name="com.google.android.apps.muzei.api.MuzeiArtSource"/>
</intent-filter>

<meta-data
android:name="color"
android:value="@color/colorPrimary"/>
</service>

<!-- Wearable Google Wearable Api Data Service -->
<service
android:name=".wearable.WearableDataService">
</service>
</application>

</manifest>
43 changes: 23 additions & 20 deletions app/src/main/java/io/github/protino/activity/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class MainActivity extends AppCompatActivity implements ForecastFragment.
private static boolean mTwoPane;
private String mLocation;

//Lifecycle start
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -69,6 +70,25 @@ protected void onCreate(Bundle savedInstanceState) {
SunshineSyncAdapter.initializeSyncAdapter(this);
}

@Override
protected void onResume() {
super.onResume();

String location = Utility.getPreferredLocation(this);
// update the location in our second pane using the fragment manager
if (location != null && !location.equals(mLocation)) {
ForecastFragment ff = (ForecastFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_forecast);
if (null != ff) {
ff.onLocationChanged();
}
DetailFragment df = (DetailFragment) getSupportFragmentManager().findFragmentByTag(DETAIL_FRAGMENT_TAG);
if (null != df) {
df.onLocationChanged(location);
}
mLocation = location;
}
}
//Lifecycle end

@Override
protected void onSaveInstanceState(Bundle outState) {
Expand All @@ -91,6 +111,9 @@ public boolean onOptionsItemSelected(MenuItem item) {
case R.id.action_map:
openPreferredLocationInMap();
return true;
case R.id.action_refresh:
SunshineSyncAdapter.syncImmediately(this);
return true;
}
return super.onOptionsItemSelected(item);
}
Expand All @@ -111,26 +134,6 @@ private void openPreferredLocationInMap() {
}
}


@Override
protected void onResume() {
super.onResume();

String location = Utility.getPreferredLocation(this);
// update the location in our second pane using the fragment manager
if (location != null && !location.equals(mLocation)) {
ForecastFragment ff = (ForecastFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_forecast);
if (null != ff) {
ff.onLocationChanged();
}
DetailFragment df = (DetailFragment) getSupportFragmentManager().findFragmentByTag(DETAIL_FRAGMENT_TAG);
if (null != df) {
df.onLocationChanged(location);
}
mLocation = location;
}
}

@Override
public void onItemSelected(Uri dateUri, ForecastAdapter.ViewHolder viewHolder) {
if (mTwoPane) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import io.github.protino.activity.MainActivity;
import io.github.protino.data.WeatherContract;
import io.github.protino.muzei.WeatherMuzeiSource;
import io.github.protino.wearable.WearableDataService;


public class SunshineSyncAdapter extends AbstractThreadedSyncAdapter {
Expand Down Expand Up @@ -125,7 +126,7 @@ public static void syncImmediately(Context context) {
* @param context The context used to access the account service
* @return a fake account.
*/
public static Account getSyncAccount(Context context) {
private static Account getSyncAccount(Context context) {
// Get an instance of the Android account manager
AccountManager accountManager =
(AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
Expand Down Expand Up @@ -451,7 +452,6 @@ private void getWeatherDataFromJson(String forecastJsonStr,
cVVector.add(weatherValues);
}

int inserted = 0;
// add to database
if (cVVector.size() > 0) {
ContentValues[] cvArray = new ContentValues[cVVector.size()];
Expand All @@ -465,6 +465,7 @@ private void getWeatherDataFromJson(String forecastJsonStr,

updateWidgets();
updateMuzei();
updateWearable();
notifyWeather();
}
Log.d(LOG_TAG, "Sync Complete. " + cVVector.size() + " Inserted");
Expand All @@ -477,6 +478,12 @@ private void getWeatherDataFromJson(String forecastJsonStr,
}
}

private void updateWearable() {
Context context = getContext();
context.startService(new Intent(context, WearableDataService.class));
Log.d(LOG_TAG, "updateWearable: service started");
}

private void updateWidgets() {
Context context = getContext();
Intent intent = new Intent(ACTION_DATA_UPDATED)
Expand Down
Loading

0 comments on commit d99a4f4

Please sign in to comment.