1 /*
2  * Copyright (C) 2014 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.v4.app;
18 
19 import android.content.ClipData;
20 import android.content.ClipDescription;
21 import android.content.Intent;
22 import android.os.Bundle;
23 
24 class RemoteInputCompatJellybean {
25     /** Label used to denote the clip data type used for remote input transport */
26     public static final String RESULTS_CLIP_LABEL = "android.remoteinput.results";
27 
28     /** Extra added to a clip data intent object to hold the results bundle. */
29     public static final String EXTRA_RESULTS_DATA = "android.remoteinput.resultsData";
30 
31     private static final String KEY_RESULT_KEY = "resultKey";
32     private static final String KEY_LABEL = "label";
33     private static final String KEY_CHOICES = "choices";
34     private static final String KEY_ALLOW_FREE_FORM_INPUT = "allowFreeFormInput";
35     private static final String KEY_EXTRAS = "extras";
36 
fromBundle(Bundle data, RemoteInputCompatBase.RemoteInput.Factory factory)37     static RemoteInputCompatBase.RemoteInput fromBundle(Bundle data,
38             RemoteInputCompatBase.RemoteInput.Factory factory) {
39         return factory.build(data.getString(KEY_RESULT_KEY),
40                 data.getCharSequence(KEY_LABEL),
41                 data.getCharSequenceArray(KEY_CHOICES),
42                 data.getBoolean(KEY_ALLOW_FREE_FORM_INPUT),
43                 data.getBundle(KEY_EXTRAS));
44     }
45 
toBundle(RemoteInputCompatBase.RemoteInput remoteInput)46     static Bundle toBundle(RemoteInputCompatBase.RemoteInput remoteInput) {
47         Bundle data = new Bundle();
48         data.putString(KEY_RESULT_KEY, remoteInput.getResultKey());
49         data.putCharSequence(KEY_LABEL, remoteInput.getLabel());
50         data.putCharSequenceArray(KEY_CHOICES, remoteInput.getChoices());
51         data.putBoolean(KEY_ALLOW_FREE_FORM_INPUT, remoteInput.getAllowFreeFormInput());
52         data.putBundle(KEY_EXTRAS, remoteInput.getExtras());
53         return data;
54     }
55 
fromBundleArray(Bundle[] bundles, RemoteInputCompatBase.RemoteInput.Factory factory)56     static RemoteInputCompatBase.RemoteInput[] fromBundleArray(Bundle[] bundles,
57             RemoteInputCompatBase.RemoteInput.Factory factory) {
58         if (bundles == null) {
59             return null;
60         }
61         RemoteInputCompatBase.RemoteInput[] remoteInputs = factory.newArray(bundles.length);
62         for (int i = 0; i < bundles.length; i++) {
63             remoteInputs[i] = fromBundle(bundles[i], factory);
64         }
65         return remoteInputs;
66     }
67 
toBundleArray(RemoteInputCompatBase.RemoteInput[] remoteInputs)68     static Bundle[] toBundleArray(RemoteInputCompatBase.RemoteInput[] remoteInputs) {
69         if (remoteInputs == null) {
70             return null;
71         }
72         Bundle[] bundles = new Bundle[remoteInputs.length];
73         for (int i = 0; i < remoteInputs.length; i++) {
74             bundles[i] = toBundle(remoteInputs[i]);
75         }
76         return bundles;
77     }
78 
getResultsFromIntent(Intent intent)79     static Bundle getResultsFromIntent(Intent intent) {
80         ClipData clipData = intent.getClipData();
81         if (clipData == null) {
82             return null;
83         }
84         ClipDescription clipDescription = clipData.getDescription();
85         if (!clipDescription.hasMimeType(ClipDescription.MIMETYPE_TEXT_INTENT)) {
86             return null;
87         }
88         if (clipDescription.getLabel().equals(RESULTS_CLIP_LABEL)) {
89             return clipData.getItemAt(0).getIntent().getExtras().getParcelable(EXTRA_RESULTS_DATA);
90         }
91         return null;
92     }
93 
addResultsToIntent(RemoteInputCompatBase.RemoteInput[] remoteInputs, Intent intent, Bundle results)94     static void addResultsToIntent(RemoteInputCompatBase.RemoteInput[] remoteInputs, Intent intent,
95             Bundle results) {
96         Bundle resultsBundle = new Bundle();
97         for (RemoteInputCompatBase.RemoteInput remoteInput : remoteInputs) {
98             Object result = results.get(remoteInput.getResultKey());
99             if (result instanceof CharSequence) {
100                 resultsBundle.putCharSequence(remoteInput.getResultKey(), (CharSequence) result);
101             }
102         }
103         Intent clipIntent = new Intent();
104         clipIntent.putExtra(EXTRA_RESULTS_DATA, resultsBundle);
105         intent.setClipData(ClipData.newIntent(RESULTS_CLIP_LABEL, clipIntent));
106     }
107 }
108