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.googlecode.android_scripting; 18 19 import android.content.Context; 20 import android.content.Intent; 21 22 import com.googlecode.android_scripting.activity.FutureActivity; 23 import com.googlecode.android_scripting.future.FutureActivityTask; 24 25 import java.util.Map; 26 import java.util.concurrent.ConcurrentHashMap; 27 import java.util.concurrent.atomic.AtomicInteger; 28 29 public class FutureActivityTaskExecutor { 30 31 private final Context mContext; 32 private final Map<Integer, FutureActivityTask<?>> mTaskMap = 33 new ConcurrentHashMap<Integer, FutureActivityTask<?>>(); 34 private final AtomicInteger mIdGenerator = new AtomicInteger(0); 35 FutureActivityTaskExecutor(Context context)36 public FutureActivityTaskExecutor(Context context) { 37 mContext = context; 38 } 39 execute(FutureActivityTask<?> task)40 public void execute(FutureActivityTask<?> task) { 41 int id = mIdGenerator.incrementAndGet(); 42 mTaskMap.put(id, task); 43 launchHelper(id); 44 } 45 getTask(int id)46 public FutureActivityTask<?> getTask(int id) { 47 return mTaskMap.remove(id); 48 } 49 launchHelper(int id)50 private void launchHelper(int id) { 51 Intent helper = new Intent(mContext, FutureActivity.class); 52 helper.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK); 53 helper.putExtra(Constants.EXTRA_TASK_ID, id); 54 mContext.startActivity(helper); 55 } 56 } 57