1 /*
2  * Copyright (C) 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 com.android.voicemail.impl.scheduling;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.os.Bundle;
22 import android.support.annotation.NonNull;
23 import com.android.voicemail.impl.VvmLog;
24 
25 /** Common operations on {@link Task} */
26 final class Tasks {
27 
Tasks()28   private Tasks() {}
29 
30   static final String EXTRA_CLASS_NAME = "extra_class_name";
31 
32   /**
33    * Create a task from a bundle. The bundle is created either with {@link #toBundle(Task)} or
34    * {@link #createIntent(Context, Class)} from the target {@link Task}
35    */
36   @NonNull
createTask(Context context, Bundle extras)37   public static Task createTask(Context context, Bundle extras) {
38     // The extra contains custom parcelables which cannot be unmarshalled by the framework class
39     // loader.
40     extras.setClassLoader(context.getClassLoader());
41     String className = extras.getString(EXTRA_CLASS_NAME);
42     VvmLog.i("Task.createTask", "create task:" + className);
43     if (className == null) {
44       throw new IllegalArgumentException("EXTRA_CLASS_NAME expected");
45     }
46     try {
47       Task task = (Task) Class.forName(className).getDeclaredConstructor().newInstance();
48       task.onCreate(context, extras);
49       return task;
50     } catch (ReflectiveOperationException e) {
51       throw new IllegalArgumentException(e);
52     }
53   }
54 
55   /**
56    * Serializes necessary states to a bundle that can be used to restore the task with {@link
57    * #createTask(Context, Bundle)}
58    */
toBundle(Task task)59   public static Bundle toBundle(Task task) {
60     Bundle result = task.toBundle();
61     result.putString(EXTRA_CLASS_NAME, task.getClass().getName());
62     return result;
63   }
64 
65   /**
66    * Create an intent that when called with {@link Context#startService(Intent)}, will queue the
67    * <code>task</code>. Implementations of {@link Task} should use the result of this and fill in
68    * necessary information.
69    */
createIntent(Context context, Class<? extends Task> task)70   public static Intent createIntent(Context context, Class<? extends Task> task) {
71     Intent intent = new Intent(context, TaskReceiver.class);
72     intent.setPackage(context.getPackageName());
73     intent.putExtra(EXTRA_CLASS_NAME, task.getName());
74     return intent;
75   }
76 }
77