OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.components.background_task_scheduler; |
| 6 |
| 7 import android.annotation.TargetApi; |
| 8 import android.os.Build; |
| 9 import android.os.Bundle; |
| 10 import android.os.PersistableBundle; |
| 11 import android.support.annotation.NonNull; |
| 12 |
| 13 import java.util.HashSet; |
| 14 import java.util.Set; |
| 15 |
| 16 /** |
| 17 * Converts from {@link Bundle} to {@link PersistableBundle}. |
| 18 * |
| 19 * The {@link android.app.job.JobScheduler} API requires the use of {@link Persi
stableBundle} in |
| 20 * the {@link android.app.job.JobInfo}, but that was added in API 21 (Lollipop).
A later change |
| 21 * related to booleans means that this class required API 22 (Lollipop MR1). |
| 22 * the API of the {@link BackgroundTaskScheduler} needs to take a {@link Bundle}
, but convert it to |
| 23 * a {@link PersistableBundle} in the case of using {@link android.app.job.JobSc
heduler} as the |
| 24 * backend for scheduling. |
| 25 */ |
| 26 @TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1) |
| 27 class BundleToPersistableBundleConverter { |
| 28 /** |
| 29 * A Result which contains the resulting {@link PersistableBundle} after a c
onversion, and also |
| 30 * contains a set of all the failed keys. |
| 31 */ |
| 32 static class Result { |
| 33 @NonNull |
| 34 private final PersistableBundle mBundle; |
| 35 @NonNull |
| 36 private final Set<String> mFailedKeys; |
| 37 |
| 38 private Result(@NonNull PersistableBundle bundle, @NonNull Set<String> f
ailedKeys) { |
| 39 mBundle = bundle; |
| 40 mFailedKeys = failedKeys; |
| 41 } |
| 42 |
| 43 boolean hasErrors() { |
| 44 return mFailedKeys.size() > 0; |
| 45 } |
| 46 |
| 47 @NonNull |
| 48 PersistableBundle getPersistableBundle() { |
| 49 return mBundle; |
| 50 } |
| 51 |
| 52 @NonNull |
| 53 Set<String> getFailedKeys() { |
| 54 return mFailedKeys; |
| 55 } |
| 56 |
| 57 String getFailedKeysErrorString() { |
| 58 StringBuilder sb = new StringBuilder(); |
| 59 sb.append("{"); |
| 60 boolean first = true; |
| 61 for (String key : mFailedKeys) { |
| 62 if (!first) sb.append(", "); |
| 63 first = false; |
| 64 |
| 65 sb.append(key); |
| 66 } |
| 67 sb.append("}"); |
| 68 return sb.toString(); |
| 69 } |
| 70 } |
| 71 |
| 72 /** |
| 73 * Copy all entries in the {@link Bundle} that can be part of a {@link Persi
stableBundle}. |
| 74 * |
| 75 * @param bundle the {@link Bundle} to convert. |
| 76 * @return a result object contain the resulting {@link PersistableBundle} a
nd whether any of |
| 77 * the keys failed. |
| 78 */ |
| 79 static Result convert(Bundle bundle) { |
| 80 PersistableBundle persistableBundle = new PersistableBundle(); |
| 81 Set<String> failedKeys = new HashSet<>(); |
| 82 for (String key : bundle.keySet()) { |
| 83 Object obj = bundle.get(key); |
| 84 if (obj == null) { |
| 85 persistableBundle.putString(key, null); |
| 86 } else if (obj instanceof Boolean) { |
| 87 persistableBundle.putBoolean(key, (Boolean) obj); |
| 88 } else if (obj instanceof boolean[]) { |
| 89 persistableBundle.putBooleanArray(key, (boolean[]) obj); |
| 90 } else if (obj instanceof Double) { |
| 91 persistableBundle.putDouble(key, (Double) obj); |
| 92 } else if (obj instanceof double[]) { |
| 93 persistableBundle.putDoubleArray(key, (double[]) obj); |
| 94 } else if (obj instanceof Integer) { |
| 95 persistableBundle.putInt(key, (Integer) obj); |
| 96 } else if (obj instanceof int[]) { |
| 97 persistableBundle.putIntArray(key, (int[]) obj); |
| 98 } else if (obj instanceof Long) { |
| 99 persistableBundle.putLong(key, (Long) obj); |
| 100 } else if (obj instanceof long[]) { |
| 101 persistableBundle.putLongArray(key, (long[]) obj); |
| 102 } else if (obj instanceof String) { |
| 103 persistableBundle.putString(key, (String) obj); |
| 104 } else if (obj instanceof String[]) { |
| 105 persistableBundle.putStringArray(key, (String[]) obj); |
| 106 } else { |
| 107 failedKeys.add(key); |
| 108 } |
| 109 } |
| 110 return new Result(persistableBundle, failedKeys); |
| 111 } |
| 112 } |
OLD | NEW |