Home | History | Annotate | Download | only in activity
      1 /*
      2  * Copyright 2018 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package androidx.wear.activity;
     18 
     19 import android.annotation.TargetApi;
     20 import android.app.Activity;
     21 import android.content.Intent;
     22 import android.os.Build;
     23 import android.os.Bundle;
     24 import android.util.SparseIntArray;
     25 
     26 import androidx.wear.R;
     27 import androidx.wear.widget.ConfirmationOverlay;
     28 
     29 /**
     30  * This Activity is used to display confirmation animations after the user completes an action on
     31  * the wearable. There are three types of confirmations: Success: the action was completed
     32  * successfully on the wearable. Failure: the action failed to complete. Open on Phone: the action
     33  * has caused something to display on the phone, or in order to complete the action, the user will
     34  * need to go to their phone to continue.
     35  *
     36  * <p>It is the responsibility of the wearable application developer to determine whether the action
     37  * has succeeded, failed, or requires the user to go to their phone, and trigger the appropriate
     38  * confirmation.
     39  *
     40  * <p>To configure the confirmation according to the result of the action, set the extra {@link
     41  * #EXTRA_ANIMATION_TYPE} to one of the following values:
     42  *
     43  * <dl>
     44  * <dt>{@link #SUCCESS_ANIMATION}
     45  * <dd>Displays a positive confirmation animation with an optional message.
     46  * <dt>{@link #OPEN_ON_PHONE_ANIMATION}
     47  * <dd>Displays an animation indicating an action has been sent to a paired device.
     48  * <dt>{@link #FAILURE_ANIMATION}
     49  * <dd>Displays a generic failure page with an optional message.
     50  * </dl>
     51  *
     52  * An optional message, included in the extra {@link #EXTRA_MESSAGE} will be displayed horizontally
     53  * centered below the animation.
     54  */
     55 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
     56 public class ConfirmationActivity extends Activity {
     57 
     58     public static final String EXTRA_MESSAGE = "androidx.wear.activity.extra.MESSAGE";
     59     public static final String EXTRA_ANIMATION_TYPE =
     60             "androidx.wear.activity.extra.ANIMATION_TYPE";
     61 
     62     public static final int SUCCESS_ANIMATION = 1;
     63     public static final int OPEN_ON_PHONE_ANIMATION = 2;
     64     public static final int FAILURE_ANIMATION = 3;
     65 
     66     private static final SparseIntArray CONFIRMATION_OVERLAY_TYPES = new SparseIntArray();
     67 
     68     static {
     69         CONFIRMATION_OVERLAY_TYPES.append(SUCCESS_ANIMATION, ConfirmationOverlay.SUCCESS_ANIMATION);
     70         CONFIRMATION_OVERLAY_TYPES.append(
     71                 OPEN_ON_PHONE_ANIMATION, ConfirmationOverlay.OPEN_ON_PHONE_ANIMATION);
     72         CONFIRMATION_OVERLAY_TYPES.append(FAILURE_ANIMATION, ConfirmationOverlay.FAILURE_ANIMATION);
     73     }
     74 
     75     @Override
     76     public void onCreate(Bundle savedInstanceState) {
     77         super.onCreate(savedInstanceState);
     78         setTheme(R.style.ConfirmationActivity);
     79 
     80         Intent intent = getIntent();
     81 
     82         int requestedType = intent.getIntExtra(EXTRA_ANIMATION_TYPE, SUCCESS_ANIMATION);
     83         if (CONFIRMATION_OVERLAY_TYPES.indexOfKey(requestedType) < 0) {
     84             throw new IllegalArgumentException("Unknown type of animation: " + requestedType);
     85         }
     86 
     87         @ConfirmationOverlay.OverlayType int type = CONFIRMATION_OVERLAY_TYPES.get(requestedType);
     88         String message = intent.getStringExtra(EXTRA_MESSAGE);
     89 
     90         new ConfirmationOverlay()
     91                 .setType(type)
     92                 .setMessage(message)
     93                 .setFinishedAnimationListener(
     94                         new ConfirmationOverlay.OnAnimationFinishedListener() {
     95                             @Override
     96                             public void onAnimationFinished() {
     97                                 ConfirmationActivity.this.onAnimationFinished();
     98                             }
     99                         })
    100                 .showOn(this);
    101     }
    102 
    103     /**
    104      * Override this method if you wish to provide different than out-of-the-box behavior when the
    105      * confirmation animation finishes. By default this method will finish the ConfirmationActivity.
    106      */
    107     protected void onAnimationFinished() {
    108         finish();
    109     }
    110 }
    111