Steve McKay | f2c8b0d | 2015-09-23 15:44:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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.documentsui; |
| 18 | |
Steve McKay | 6c8bfb7 | 2015-10-05 12:32:46 -0700 | [diff] [blame] | 19 | import static com.android.documentsui.Shared.DEBUG; |
| 20 | |
Steve McKay | f2c8b0d | 2015-09-23 15:44:24 -0700 | [diff] [blame] | 21 | import android.app.Activity; |
| 22 | import android.app.ActivityManager; |
| 23 | import android.app.ActivityManager.AppTask; |
Steve McKay | f2c8b0d | 2015-09-23 15:44:24 -0700 | [diff] [blame] | 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
| 26 | import android.net.Uri; |
| 27 | import android.os.Bundle; |
| 28 | import android.support.annotation.Nullable; |
Steve McKay | 6c8bfb7 | 2015-10-05 12:32:46 -0700 | [diff] [blame] | 29 | import android.util.Log; |
Steve McKay | f2c8b0d | 2015-09-23 15:44:24 -0700 | [diff] [blame] | 30 | |
| 31 | import java.util.List; |
| 32 | |
| 33 | /** |
| 34 | * Provides FilesActivity task grouping support. This allows multiple FilesActivities to be |
| 35 | * launched (a behavior imparted by way of {@code documentLaunchMode="intoExisting"} and |
| 36 | * our use of pseudo document {@link Uri}s. This also lets us move an existing task |
| 37 | * to the foreground when a suitable task exists. |
| 38 | * |
| 39 | * Requires that {@code documentLaunchMode="intoExisting"} be set on target activity. |
| 40 | * |
| 41 | */ |
| 42 | public class LauncherActivity extends Activity { |
| 43 | |
Steve McKay | 6c8bfb7 | 2015-10-05 12:32:46 -0700 | [diff] [blame] | 44 | private static final String LAUNCH_CONTROL_AUTHORITY = "com.android.documentsui.launchControl"; |
| 45 | private static final String TAG = "LauncherActivity"; |
Steve McKay | f2c8b0d | 2015-09-23 15:44:24 -0700 | [diff] [blame] | 46 | |
| 47 | @Override |
| 48 | protected void onCreate(Bundle savedInstanceState) { |
| 49 | super.onCreate(savedInstanceState); |
| 50 | |
| 51 | ActivityManager activities = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); |
Steve McKay | f2c8b0d | 2015-09-23 15:44:24 -0700 | [diff] [blame] | 52 | |
Steve McKay | 6c8bfb7 | 2015-10-05 12:32:46 -0700 | [diff] [blame] | 53 | Intent intent = findTask(activities); |
| 54 | if (intent != null) { |
| 55 | restoreTask(intent); |
Steve McKay | f2c8b0d | 2015-09-23 15:44:24 -0700 | [diff] [blame] | 56 | } else { |
Steve McKay | 6c8bfb7 | 2015-10-05 12:32:46 -0700 | [diff] [blame] | 57 | startTask(); |
Steve McKay | f2c8b0d | 2015-09-23 15:44:24 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | finish(); |
| 61 | } |
| 62 | |
Steve McKay | 6c8bfb7 | 2015-10-05 12:32:46 -0700 | [diff] [blame] | 63 | private @Nullable Intent findTask(ActivityManager activities) { |
| 64 | List<AppTask> tasks = activities.getAppTasks(); |
| 65 | for (AppTask task : tasks) { |
| 66 | Intent intent = task.getTaskInfo().baseIntent; |
| 67 | Uri uri = intent.getData(); |
| 68 | if (isLaunchUri(uri)) { |
| 69 | return intent; |
| 70 | } |
| 71 | } |
| 72 | return null; |
| 73 | } |
| 74 | |
| 75 | private void startTask() { |
Steve McKay | f2c8b0d | 2015-09-23 15:44:24 -0700 | [diff] [blame] | 76 | Intent intent = createLaunchIntent(this); |
Steve McKay | df5add4 | 2016-01-28 12:02:57 -0800 | [diff] [blame] | 77 | |
| 78 | // Forward any flags from the original intent. |
| 79 | intent.setFlags(getIntent().getFlags()); |
Steve McKay | 6c8bfb7 | 2015-10-05 12:32:46 -0700 | [diff] [blame] | 80 | if (DEBUG) Log.d(TAG, "Starting new task > " + intent.getData()); |
Steve McKay | f2c8b0d | 2015-09-23 15:44:24 -0700 | [diff] [blame] | 81 | startActivity(intent); |
| 82 | } |
| 83 | |
Steve McKay | 6c8bfb7 | 2015-10-05 12:32:46 -0700 | [diff] [blame] | 84 | private void restoreTask(Intent intent) { |
| 85 | if (DEBUG) Log.d(TAG, "Restoring existing task > " + intent.getData()); |
| 86 | // TODO: This doesn't appear to restore a task once it has stopped running. |
| 87 | startActivity(intent); |
Steve McKay | f2c8b0d | 2015-09-23 15:44:24 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Steve McKay | df5add4 | 2016-01-28 12:02:57 -0800 | [diff] [blame] | 90 | static final Intent createLaunchIntent(Context context) { |
Steve McKay | f2c8b0d | 2015-09-23 15:44:24 -0700 | [diff] [blame] | 91 | Intent intent = new Intent(context, FilesActivity.class); |
| 92 | intent.setData(buildLaunchUri()); |
| 93 | return intent; |
| 94 | } |
| 95 | |
| 96 | private static Uri buildLaunchUri() { |
| 97 | return new Uri.Builder() |
| 98 | .authority(LAUNCH_CONTROL_AUTHORITY) |
| 99 | .fragment(String.valueOf(System.currentTimeMillis())) |
| 100 | .build(); |
| 101 | } |
| 102 | |
| 103 | static boolean isLaunchUri(@Nullable Uri uri) { |
Steve McKay | 6c8bfb7 | 2015-10-05 12:32:46 -0700 | [diff] [blame] | 104 | boolean result = uri != null && LAUNCH_CONTROL_AUTHORITY.equals(uri.getAuthority()); |
| 105 | return result; |
Steve McKay | f2c8b0d | 2015-09-23 15:44:24 -0700 | [diff] [blame] | 106 | } |
| 107 | } |