Chromium Code Reviews| 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). This means that | |
| 21 * the API of the {@link BackgroundTaskScheduler} needs to take a {@link Bundle} , but convert it to | |
| 22 * a {@link PersistableBundle} in the case of using {@link android.app.job.JobSc heduler} as the | |
| 23 * backend for scheduling. | |
| 24 */ | |
| 25 @TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1) | |
|
Peter Beverloo
2017/02/24 18:07:10
nit: docs say API 21, annotation says 22. Is this
nyquist
2017/02/24 23:41:12
Yes :-( Updated comment.
| |
| 26 class BundleToPersistableBundleConverter { | |
| 27 /** | |
| 28 * A Result which contains the resulting {@link PersistableBundle} after a c onversion, and also | |
| 29 * contains a set of all the failed keys. | |
| 30 */ | |
| 31 static class Result { | |
| 32 @NonNull | |
| 33 private final PersistableBundle mBundle; | |
| 34 @NonNull | |
| 35 private final Set<String> mFailedKeys; | |
| 36 | |
| 37 private Result(@NonNull PersistableBundle bundle, @NonNull Set<String> f ailedKeys) { | |
|
Peter Beverloo
2017/02/24 18:07:12
nit: Up to the Java crowd, but personally I find i
nyquist
2017/02/24 23:41:12
You have a good point. I guess I was just uncertai
| |
| 38 mBundle = bundle; | |
| 39 mFailedKeys = failedKeys; | |
| 40 } | |
| 41 | |
| 42 boolean hasErrors() { | |
| 43 return mFailedKeys.size() > 0; | |
| 44 } | |
| 45 | |
| 46 @NonNull | |
| 47 PersistableBundle getPersistableBundle() { | |
| 48 return mBundle; | |
| 49 } | |
| 50 | |
| 51 @NonNull | |
| 52 Set<String> getFailedKeys() { | |
| 53 return mFailedKeys; | |
| 54 } | |
| 55 | |
| 56 String getFailedKeysErrorString() { | |
| 57 StringBuilder sb = new StringBuilder(); | |
| 58 sb.append("{"); | |
| 59 boolean first = true; | |
| 60 for (String key : mFailedKeys) { | |
| 61 if (!first) sb.append(", "); | |
| 62 first = false; | |
| 63 | |
| 64 sb.append(key); | |
| 65 } | |
| 66 sb.append("}"); | |
| 67 return sb.toString(); | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 /** | |
| 72 * Copy all entries in the {@link Bundle} that can be part of a {@link Persi stableBundle}. | |
| 73 * @param bundle the {@link Bundle} to convert. | |
| 74 * @return a result object contain the resulting {@link PersistableBundle} a nd whether any of | |
| 75 * the keys failed. | |
| 76 */ | |
| 77 static Result convert(Bundle bundle) { | |
| 78 PersistableBundle persistableBundle = new PersistableBundle(); | |
| 79 Set<String> failedKeys = new HashSet<>(); | |
| 80 for (String key : bundle.keySet()) { | |
| 81 Object obj = bundle.get(key); | |
| 82 if (obj instanceof Boolean) { | |
| 83 persistableBundle.putBoolean(key, (Boolean) obj); | |
| 84 } else if (obj instanceof boolean[]) { | |
| 85 persistableBundle.putBooleanArray(key, (boolean[]) obj); | |
| 86 } else if (obj instanceof Double) { | |
| 87 persistableBundle.putDouble(key, (Double) obj); | |
| 88 } else if (obj instanceof double[]) { | |
| 89 persistableBundle.putDoubleArray(key, (double[]) obj); | |
| 90 } else if (obj instanceof Integer) { | |
| 91 persistableBundle.putInt(key, (Integer) obj); | |
| 92 } else if (obj instanceof int[]) { | |
| 93 persistableBundle.putIntArray(key, (int[]) obj); | |
| 94 } else if (obj instanceof Long) { | |
| 95 persistableBundle.putLong(key, (Long) obj); | |
| 96 } else if (obj instanceof long[]) { | |
| 97 persistableBundle.putLongArray(key, (long[]) obj); | |
| 98 } else if (obj instanceof String) { | |
| 99 persistableBundle.putString(key, (String) obj); | |
| 100 } else if (obj instanceof String[]) { | |
| 101 persistableBundle.putStringArray(key, (String[]) obj); | |
| 102 } else { | |
|
Peter Beverloo
2017/02/24 20:29:44
This needs to be able to handle NULL values. I sug
nyquist
2017/02/24 23:41:12
Done. Also added test for this.
| |
| 103 failedKeys.add(key); | |
| 104 } | |
| 105 } | |
| 106 return new Result(persistableBundle, failedKeys); | |
| 107 } | |
| 108 } | |
| OLD | NEW |