Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright 2017 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 android.support.mediacompat.testlib.util;
     18 
     19 import android.content.ComponentName;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.os.Build;
     23 import android.os.Bundle;
     24 import android.os.Parcelable;
     25 
     26 import java.util.ArrayList;
     27 
     28 /**
     29  * Methods and constants used for sending intent between client and service apps.
     30  */
     31 public class IntentUtil {
     32 
     33     public static final String SERVICE_PACKAGE_NAME = "android.support.mediacompat.service.test";
     34     public static final String CLIENT_PACKAGE_NAME = "android.support.mediacompat.client.test";
     35 
     36     public static final ComponentName SERVICE_RECEIVER_COMPONENT_NAME = new ComponentName(
     37             SERVICE_PACKAGE_NAME, "android.support.mediacompat.service.ServiceBroadcastReceiver");
     38     public static final ComponentName CLIENT_RECEIVER_COMPONENT_NAME = new ComponentName(
     39             CLIENT_PACKAGE_NAME, "android.support.mediacompat.client.ClientBroadcastReceiver");
     40 
     41     public static final String ACTION_CALL_MEDIA_BROWSER_SERVICE_METHOD =
     42             "android.support.mediacompat.service.action.CALL_MEDIA_BROWSER_SERVICE_METHOD";
     43     public static final String ACTION_CALL_MEDIA_SESSION_METHOD =
     44             "android.support.mediacompat.service.action.CALL_MEDIA_SESSION_METHOD";
     45     public static final String ACTION_CALL_MEDIA_CONTROLLER_METHOD =
     46             "android.support.mediacompat.client.action.CALL_MEDIA_CONTROLLER_METHOD";
     47     public static final String ACTION_CALL_TRANSPORT_CONTROLS_METHOD =
     48             "android.support.mediacompat.client.action.CALL_TRANSPORT_CONTROLS_METHOD";
     49 
     50     public static final String KEY_METHOD_ID = "method_id";
     51     public static final String KEY_ARGUMENT = "argument";
     52     public static final String KEY_SESSION_TOKEN = "session_token";
     53 
     54     /**
     55      * Calls a method of MediaBrowserService. Used by client app.
     56      */
     57     public static void callMediaBrowserServiceMethod(int methodId, Object arg, Context context) {
     58         Intent intent = createIntent(SERVICE_RECEIVER_COMPONENT_NAME, methodId, arg);
     59         intent.setAction(ACTION_CALL_MEDIA_BROWSER_SERVICE_METHOD);
     60         if (Build.VERSION.SDK_INT >= 16) {
     61             intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
     62         }
     63         context.sendBroadcast(intent);
     64     }
     65 
     66     /**
     67      * Calls a method of MediaSession. Used by client app.
     68      */
     69     public static void callMediaSessionMethod(int methodId, Object arg, Context context) {
     70         Intent intent = createIntent(SERVICE_RECEIVER_COMPONENT_NAME, methodId, arg);
     71         intent.setAction(ACTION_CALL_MEDIA_SESSION_METHOD);
     72         if (Build.VERSION.SDK_INT >= 16) {
     73             intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
     74         }
     75         context.sendBroadcast(intent);
     76     }
     77 
     78     /**
     79      * Calls a method of MediaController. Used by service app.
     80      */
     81     public static void callMediaControllerMethod(
     82             int methodId, Object arg, Context context, Parcelable token) {
     83         Intent intent = createIntent(CLIENT_RECEIVER_COMPONENT_NAME, methodId, arg);
     84         intent.setAction(ACTION_CALL_MEDIA_CONTROLLER_METHOD);
     85         intent.putExtra(KEY_SESSION_TOKEN, token);
     86         if (Build.VERSION.SDK_INT >= 16) {
     87             intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
     88         }
     89         context.sendBroadcast(intent);
     90     }
     91 
     92     /**
     93      * Calls a method of TransportControls. Used by service app.
     94      */
     95     public static void callTransportControlsMethod(
     96             int methodId, Object arg, Context context, Parcelable token) {
     97         Intent intent = createIntent(CLIENT_RECEIVER_COMPONENT_NAME, methodId, arg);
     98         intent.setAction(ACTION_CALL_TRANSPORT_CONTROLS_METHOD);
     99         intent.putExtra(KEY_SESSION_TOKEN, token);
    100         if (Build.VERSION.SDK_INT >= 16) {
    101             intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
    102         }
    103         context.sendBroadcast(intent);
    104     }
    105 
    106     private static Intent createIntent(ComponentName componentName, int methodId, Object arg) {
    107         Intent intent = new Intent();
    108         intent.setComponent(componentName);
    109         intent.putExtra(KEY_METHOD_ID, methodId);
    110 
    111         if (arg instanceof String) {
    112             intent.putExtra(KEY_ARGUMENT, (String) arg);
    113         } else if (arg instanceof Integer) {
    114             intent.putExtra(KEY_ARGUMENT, (int) arg);
    115         } else if (arg instanceof Long) {
    116             intent.putExtra(KEY_ARGUMENT, (long) arg);
    117         } else if (arg instanceof Boolean) {
    118             intent.putExtra(KEY_ARGUMENT, (boolean) arg);
    119         } else if (arg instanceof Parcelable) {
    120             intent.putExtra(KEY_ARGUMENT, (Parcelable) arg);
    121         } else if (arg instanceof ArrayList<?>) {
    122             Bundle bundle = new Bundle();
    123             bundle.putParcelableArrayList(KEY_ARGUMENT, (ArrayList<? extends Parcelable>) arg);
    124             intent.putExtras(bundle);
    125         } else if (arg instanceof Bundle) {
    126             Bundle bundle = new Bundle();
    127             bundle.putBundle(KEY_ARGUMENT, (Bundle) arg);
    128             intent.putExtras(bundle);
    129         }
    130         return intent;
    131     }
    132 
    133     private IntentUtil() {
    134     }
    135 }
    136