diff options
Diffstat (limited to 'samples/browseable/WatchFace/Wearable/src/com.example.android.wearable.watchface')
4 files changed, 179 insertions, 52 deletions
diff --git a/samples/browseable/WatchFace/Wearable/src/com.example.android.wearable.watchface/ComplicationSimpleConfigActivity.java b/samples/browseable/WatchFace/Wearable/src/com.example.android.wearable.watchface/ComplicationSimpleConfigActivity.java index 66e208c2e..6d4e521a1 100644 --- a/samples/browseable/WatchFace/Wearable/src/com.example.android.wearable.watchface/ComplicationSimpleConfigActivity.java +++ b/samples/browseable/WatchFace/Wearable/src/com.example.android.wearable.watchface/ComplicationSimpleConfigActivity.java @@ -22,6 +22,8 @@ import android.content.Intent; import android.content.res.TypedArray; import android.graphics.drawable.Drawable; import android.os.Bundle; +import android.support.wearable.complications.ComplicationHelperActivity; +import android.support.wearable.complications.ComplicationProviderInfo; import android.support.wearable.complications.ProviderChooserIntent; import android.support.wearable.view.WearableListView; import android.util.Log; @@ -64,6 +66,13 @@ public class ComplicationSimpleConfigActivity extends Activity implements protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == PROVIDER_CHOOSER_REQUEST_CODE && resultCode == RESULT_OK) { + + // Retrieves information for selected Complication provider. + ComplicationProviderInfo complicationProviderInfo = + data.getParcelableExtra(ProviderChooserIntent.EXTRA_PROVIDER_INFO); + + Log.d(TAG, "Selected Provider: " + complicationProviderInfo); + finish(); } } @@ -77,10 +86,16 @@ public class ComplicationSimpleConfigActivity extends Activity implements Integer tag = (Integer) viewHolder.itemView.getTag(); ComplicationItem complicationItem = mAdapter.getItem(tag); - startActivityForResult(ProviderChooserIntent.createProviderChooserIntent( - complicationItem.watchFace, - complicationItem.complicationId, - complicationItem.supportedTypes), PROVIDER_CHOOSER_REQUEST_CODE); + // Note: If you were previously using ProviderChooserIntent.createProviderChooserIntent() + // (now deprecated), you will want to switch to + // ComplicationHelperActivity.createProviderChooserHelperIntent() + startActivityForResult( + ComplicationHelperActivity.createProviderChooserHelperIntent( + getApplicationContext(), + complicationItem.watchFace, + complicationItem.complicationId, + complicationItem.supportedTypes), + PROVIDER_CHOOSER_REQUEST_CODE); } private List<ComplicationItem> getComplicationItems() { @@ -187,5 +202,4 @@ public class ComplicationSimpleConfigActivity extends Activity implements return mItems.get(position); } } - }
\ No newline at end of file diff --git a/samples/browseable/WatchFace/Wearable/src/com.example.android.wearable.watchface/ComplicationSimpleWatchFaceService.java b/samples/browseable/WatchFace/Wearable/src/com.example.android.wearable.watchface/ComplicationSimpleWatchFaceService.java index 9eca2c33b..4905293dd 100644 --- a/samples/browseable/WatchFace/Wearable/src/com.example.android.wearable.watchface/ComplicationSimpleWatchFaceService.java +++ b/samples/browseable/WatchFace/Wearable/src/com.example.android.wearable.watchface/ComplicationSimpleWatchFaceService.java @@ -18,6 +18,7 @@ package com.example.android.wearable.watchface; import android.app.PendingIntent; import android.content.BroadcastReceiver; +import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; @@ -35,6 +36,7 @@ import android.os.Handler; import android.os.Message; import android.support.v7.graphics.Palette; import android.support.wearable.complications.ComplicationData; +import android.support.wearable.complications.ComplicationHelperActivity; import android.support.wearable.complications.ComplicationText; import android.support.wearable.watchface.CanvasWatchFaceService; import android.support.wearable.watchface.WatchFaceService; @@ -362,16 +364,34 @@ public class ComplicationSimpleWatchFaceService extends CanvasWatchFaceService { if (Log.isLoggable(TAG, Log.DEBUG)) { Log.d(TAG, "onComplicationTap()"); } + ComplicationData complicationData = mActiveComplicationDataSparseArray.get(complicationId); - if ((complicationData != null) && (complicationData.getTapAction() != null)) { - try { - complicationData.getTapAction().send(); - } catch (PendingIntent.CanceledException e) { - Log.e(TAG, "On complication tap action error " + e); + if (complicationData != null) { + + if (complicationData.getTapAction() != null) { + try { + complicationData.getTapAction().send(); + } catch (PendingIntent.CanceledException e) { + Log.e(TAG, "On complication tap action error " + e); + } + + } else if (complicationData.getType() == ComplicationData.TYPE_NO_PERMISSION) { + + // Watch face does not have permission to receive complication data, so launch + // permission request. + ComponentName componentName = new ComponentName( + getApplicationContext(), + ComplicationSimpleWatchFaceService.class); + + Intent permissionRequestIntent = + ComplicationHelperActivity.createPermissionRequestHelperIntent( + getApplicationContext(), componentName); + + startActivity(permissionRequestIntent); } - invalidate(); + } else { if (Log.isLoggable(TAG, Log.DEBUG)) { Log.d(TAG, "No PendingIntent for complication " + complicationId + "."); @@ -551,50 +571,57 @@ public class ComplicationSimpleWatchFaceService extends CanvasWatchFaceService { complicationData = mActiveComplicationDataSparseArray.get(COMPLICATION_IDS[i]); if ((complicationData != null) - && (complicationData.isActive(currentTimeMillis)) - && (complicationData.getType() == ComplicationData.TYPE_SHORT_TEXT)) { - - ComplicationText mainText = complicationData.getShortText(); - ComplicationText subText = complicationData.getShortTitle(); - - CharSequence complicationMessage = - mainText.getText(getApplicationContext(), currentTimeMillis); - - /* In most cases you would want the subText (Title) under the mainText (Text), - * but to keep it simple for the code lab, we are concatenating them all on one - * line. - */ - if (subText != null) { - complicationMessage = TextUtils.concat( - complicationMessage, - " ", - subText.getText(getApplicationContext(), currentTimeMillis)); - } - - //Log.d(TAG, "Comp id: " + COMPLICATION_IDS[i] + "\t" + complicationMessage); - double textWidth = - mComplicationPaint.measureText( + && (complicationData.isActive(currentTimeMillis))) { + + // Both Short Text and No Permission Types can be rendered with the same code. + // No Permission will display "--" with an Intent to launch a permission prompt. + // If you want to support more types, just add a "else if" below with your + // rendering code inside. + if (complicationData.getType() == ComplicationData.TYPE_SHORT_TEXT + || complicationData.getType() == ComplicationData.TYPE_NO_PERMISSION) { + + ComplicationText mainText = complicationData.getShortText(); + ComplicationText subText = complicationData.getShortTitle(); + + CharSequence complicationMessage = + mainText.getText(getApplicationContext(), currentTimeMillis); + + /* In most cases you would want the subText (Title) under the + * mainText (Text), but to keep it simple for the code lab, we are + * concatenating them all on one line. + */ + if (subText != null) { + complicationMessage = TextUtils.concat( complicationMessage, - 0, - complicationMessage.length()); + " ", + subText.getText(getApplicationContext(), currentTimeMillis)); + } - int complicationsX; + //Log.d(TAG, "Com id: " + COMPLICATION_IDS[i] + "\t" + complicationMessage); + double textWidth = + mComplicationPaint.measureText( + complicationMessage, + 0, + complicationMessage.length()); + + int complicationsX; + + if (COMPLICATION_IDS[i] == LEFT_DIAL_COMPLICATION) { + complicationsX = (int) ((mWidth / 2) - textWidth) / 2; + } else { + // RIGHT_DIAL_COMPLICATION calculations + int offset = (int) ((mWidth / 2) - textWidth) / 2; + complicationsX = (mWidth / 2) + offset; + } - if (COMPLICATION_IDS[i] == LEFT_DIAL_COMPLICATION) { - complicationsX = (int) ((mWidth / 2) - textWidth) / 2; - } else { - // RIGHT_DIAL_COMPLICATION calculations - int offset = (int) ((mWidth / 2) - textWidth) / 2; - complicationsX = (mWidth / 2) + offset; + canvas.drawText( + complicationMessage, + 0, + complicationMessage.length(), + complicationsX, + mComplicationsY, + mComplicationPaint); } - - canvas.drawText( - complicationMessage, - 0, - complicationMessage.length(), - complicationsX, - mComplicationsY, - mComplicationPaint); } } } diff --git a/samples/browseable/WatchFace/Wearable/src/com.example.android.wearable.watchface/provider/RandomNumberProviderService.java b/samples/browseable/WatchFace/Wearable/src/com.example.android.wearable.watchface/provider/RandomNumberProviderService.java index 916f90fd9..17d03c272 100644 --- a/samples/browseable/WatchFace/Wearable/src/com.example.android.wearable.watchface/provider/RandomNumberProviderService.java +++ b/samples/browseable/WatchFace/Wearable/src/com.example.android.wearable.watchface/provider/RandomNumberProviderService.java @@ -1,5 +1,7 @@ package com.example.android.wearable.watchface.provider; +import android.app.PendingIntent; +import android.content.Intent; import android.support.wearable.complications.ComplicationData; import android.support.wearable.complications.ComplicationManager; import android.support.wearable.complications.ComplicationProviderService; @@ -42,7 +44,7 @@ public class RandomNumberProviderService extends ComplicationProviderService { @Override public void onComplicationUpdate( int complicationId, int dataType, ComplicationManager complicationManager) { - Log.d(TAG, "onComplicationUpdate()"); + Log.d(TAG, "onComplicationUpdate() id: " + complicationId); // Retrieve your data, in this case, we simply create a random number to display. @@ -51,6 +53,24 @@ public class RandomNumberProviderService extends ComplicationProviderService { String randomNumberText = String.format(Locale.getDefault(), "%d!", randomNumber); + // Create Tap Action so that the user can trigger an update by tapping the complication. + Intent updateIntent = + new Intent(getApplicationContext(), UpdateComplicationDataService.class); + updateIntent.setAction(UpdateComplicationDataService.ACTION_UPDATE_COMPLICATION); + // We pass the complication id, so we can only update the specific complication tapped. + updateIntent.putExtra(UpdateComplicationDataService.EXTRA_COMPLICATION_ID, complicationId); + + PendingIntent pendingIntent = PendingIntent.getService( + getApplicationContext(), + // Set the requestCode to the complication id. This ensures the system doesn't + // combine other PendingIntents with the same context with this one (basically it + // would then reuse the Extra you set in the initial PendingIntent). If you don't + // do this and multiple complications with your data are active, every PendingIntent + // assigned for tap, would use the same complication id (first one created). + complicationId, + updateIntent, + 0); + ComplicationData complicationData = null; switch (dataType) { @@ -60,17 +80,20 @@ public class RandomNumberProviderService extends ComplicationProviderService { .setMinValue(0) .setMaxValue(10) .setShortText(ComplicationText.plainText(randomNumberText)) + .setTapAction(pendingIntent) .build(); break; case ComplicationData.TYPE_SHORT_TEXT: complicationData = new ComplicationData.Builder(ComplicationData.TYPE_SHORT_TEXT) .setShortText(ComplicationText.plainText(randomNumberText)) + .setTapAction(pendingIntent) .build(); break; case ComplicationData.TYPE_LONG_TEXT: complicationData = new ComplicationData.Builder(ComplicationData.TYPE_LONG_TEXT) .setLongText( ComplicationText.plainText("Random Number: " + randomNumberText)) + .setTapAction(pendingIntent) .build(); break; default: diff --git a/samples/browseable/WatchFace/Wearable/src/com.example.android.wearable.watchface/provider/UpdateComplicationDataService.java b/samples/browseable/WatchFace/Wearable/src/com.example.android.wearable.watchface/provider/UpdateComplicationDataService.java new file mode 100644 index 000000000..203e491c2 --- /dev/null +++ b/samples/browseable/WatchFace/Wearable/src/com.example.android.wearable.watchface/provider/UpdateComplicationDataService.java @@ -0,0 +1,63 @@ +package com.example.android.wearable.watchface.provider; + +import android.app.IntentService; +import android.content.ComponentName; +import android.content.Intent; +import android.support.wearable.complications.ProviderUpdateRequester; +import android.util.Log; + +/** + * Simple {@link IntentService} subclass for asynchronously requesting an update for the random + * number complication (triggered via TapAction on complication). + */ +public class UpdateComplicationDataService extends IntentService { + + private static final String TAG = "UpdateCompService"; + + public static final String ACTION_UPDATE_COMPLICATION = + "com.example.android.wearable.watchface.provider.action.UPDATE_COMPLICATION"; + + public static final String EXTRA_COMPLICATION_ID = + "com.example.android.wearable.watchface.provider.action.COMPLICATION_ID"; + + public UpdateComplicationDataService() { + super("UpdateComplicationDataService"); + } + + @Override + protected void onHandleIntent(Intent intent) { + + if (intent != null) { + + final String action = intent.getAction(); + + if (ACTION_UPDATE_COMPLICATION.equals(action)) { + + int complicationId = intent.getIntExtra(EXTRA_COMPLICATION_ID, -1); + handleActionUpdateComplicationData(complicationId); + } + } + } + + /** + * Handle action UpdateComplicationData in the provided background thread with the provided + * parameters. + */ + private void handleActionUpdateComplicationData(int complicationId) { + + Log.d(TAG, "Complication id to update via service: " + complicationId); + + ComponentName componentName = + new ComponentName(getApplicationContext(), RandomNumberProviderService.class); + + ProviderUpdateRequester providerUpdateRequester = + new ProviderUpdateRequester(getApplicationContext(), componentName); + + if (complicationId > 0) { + // This method only updates the specific complication tapped on the watch, if you + // wanted to update all active complications associated with your data, you would + // call providerUpdateRequester.requestUpdateAll(). + providerUpdateRequester.requestUpdate(complicationId); + } + } +}
\ No newline at end of file |