Chromium Code Reviews| Index: components/background_task_scheduler/android/java/src/org/chromium/components/background_task_scheduler/BundleToPersistableBundleConverter.java |
| diff --git a/components/background_task_scheduler/android/java/src/org/chromium/components/background_task_scheduler/BundleToPersistableBundleConverter.java b/components/background_task_scheduler/android/java/src/org/chromium/components/background_task_scheduler/BundleToPersistableBundleConverter.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7e61b1665e4effd28544fc5f207e3999f188e569 |
| --- /dev/null |
| +++ b/components/background_task_scheduler/android/java/src/org/chromium/components/background_task_scheduler/BundleToPersistableBundleConverter.java |
| @@ -0,0 +1,108 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.components.background_task_scheduler; |
| + |
| +import android.annotation.TargetApi; |
| +import android.os.Build; |
| +import android.os.Bundle; |
| +import android.os.PersistableBundle; |
| +import android.support.annotation.NonNull; |
| + |
| +import java.util.HashSet; |
| +import java.util.Set; |
| + |
| +/** |
| + * Converts from {@link Bundle} to {@link PersistableBundle}. |
| + * |
| + * The {@link android.app.job.JobScheduler} API requires the use of {@link PersistableBundle} in |
| + * the {@link android.app.job.JobInfo}, but that was added in API 21 (Lollipop). This means that |
| + * the API of the {@link BackgroundTaskScheduler} needs to take a {@link Bundle}, but convert it to |
| + * a {@link PersistableBundle} in the case of using {@link android.app.job.JobScheduler} as the |
| + * backend for scheduling. |
| + */ |
| +@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.
|
| +class BundleToPersistableBundleConverter { |
| + /** |
| + * A Result which contains the resulting {@link PersistableBundle} after a conversion, and also |
| + * contains a set of all the failed keys. |
| + */ |
| + static class Result { |
| + @NonNull |
| + private final PersistableBundle mBundle; |
| + @NonNull |
| + private final Set<String> mFailedKeys; |
| + |
| + private Result(@NonNull PersistableBundle bundle, @NonNull Set<String> failedKeys) { |
|
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
|
| + mBundle = bundle; |
| + mFailedKeys = failedKeys; |
| + } |
| + |
| + boolean hasErrors() { |
| + return mFailedKeys.size() > 0; |
| + } |
| + |
| + @NonNull |
| + PersistableBundle getPersistableBundle() { |
| + return mBundle; |
| + } |
| + |
| + @NonNull |
| + Set<String> getFailedKeys() { |
| + return mFailedKeys; |
| + } |
| + |
| + String getFailedKeysErrorString() { |
| + StringBuilder sb = new StringBuilder(); |
| + sb.append("{"); |
| + boolean first = true; |
| + for (String key : mFailedKeys) { |
| + if (!first) sb.append(", "); |
| + first = false; |
| + |
| + sb.append(key); |
| + } |
| + sb.append("}"); |
| + return sb.toString(); |
| + } |
| + } |
| + |
| + /** |
| + * Copy all entries in the {@link Bundle} that can be part of a {@link PersistableBundle}. |
| + * @param bundle the {@link Bundle} to convert. |
| + * @return a result object contain the resulting {@link PersistableBundle} and whether any of |
| + * the keys failed. |
| + */ |
| + static Result convert(Bundle bundle) { |
| + PersistableBundle persistableBundle = new PersistableBundle(); |
| + Set<String> failedKeys = new HashSet<>(); |
| + for (String key : bundle.keySet()) { |
| + Object obj = bundle.get(key); |
| + if (obj instanceof Boolean) { |
| + persistableBundle.putBoolean(key, (Boolean) obj); |
| + } else if (obj instanceof boolean[]) { |
| + persistableBundle.putBooleanArray(key, (boolean[]) obj); |
| + } else if (obj instanceof Double) { |
| + persistableBundle.putDouble(key, (Double) obj); |
| + } else if (obj instanceof double[]) { |
| + persistableBundle.putDoubleArray(key, (double[]) obj); |
| + } else if (obj instanceof Integer) { |
| + persistableBundle.putInt(key, (Integer) obj); |
| + } else if (obj instanceof int[]) { |
| + persistableBundle.putIntArray(key, (int[]) obj); |
| + } else if (obj instanceof Long) { |
| + persistableBundle.putLong(key, (Long) obj); |
| + } else if (obj instanceof long[]) { |
| + persistableBundle.putLongArray(key, (long[]) obj); |
| + } else if (obj instanceof String) { |
| + persistableBundle.putString(key, (String) obj); |
| + } else if (obj instanceof String[]) { |
| + persistableBundle.putStringArray(key, (String[]) obj); |
| + } 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.
|
| + failedKeys.add(key); |
| + } |
| + } |
| + return new Result(persistableBundle, failedKeys); |
| + } |
| +} |