blob: 7930c28fc6c2930989660315ccd3c89441370c34 [file] [log] [blame]
Steve McKayf2c8b0d2015-09-23 15:44:24 -07001/*
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
17package com.android.documentsui;
18
Steve McKay6c8bfb72015-10-05 12:32:46 -070019import static com.android.documentsui.Shared.DEBUG;
20
Steve McKayf2c8b0d2015-09-23 15:44:24 -070021import android.app.Activity;
22import android.app.ActivityManager;
23import android.app.ActivityManager.AppTask;
Steve McKayf2c8b0d2015-09-23 15:44:24 -070024import android.content.Context;
25import android.content.Intent;
26import android.net.Uri;
27import android.os.Bundle;
28import android.support.annotation.Nullable;
Steve McKay6c8bfb72015-10-05 12:32:46 -070029import android.util.Log;
Steve McKayf2c8b0d2015-09-23 15:44:24 -070030
31import 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 */
42public class LauncherActivity extends Activity {
43
Steve McKay6c8bfb72015-10-05 12:32:46 -070044 private static final String LAUNCH_CONTROL_AUTHORITY = "com.android.documentsui.launchControl";
45 private static final String TAG = "LauncherActivity";
Steve McKayf2c8b0d2015-09-23 15:44:24 -070046
47 @Override
48 protected void onCreate(Bundle savedInstanceState) {
49 super.onCreate(savedInstanceState);
50
51 ActivityManager activities = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
Steve McKayf2c8b0d2015-09-23 15:44:24 -070052
Steve McKay6c8bfb72015-10-05 12:32:46 -070053 Intent intent = findTask(activities);
54 if (intent != null) {
55 restoreTask(intent);
Steve McKayf2c8b0d2015-09-23 15:44:24 -070056 } else {
Steve McKay6c8bfb72015-10-05 12:32:46 -070057 startTask();
Steve McKayf2c8b0d2015-09-23 15:44:24 -070058 }
59
60 finish();
61 }
62
Steve McKay6c8bfb72015-10-05 12:32:46 -070063 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 McKayf2c8b0d2015-09-23 15:44:24 -070076 Intent intent = createLaunchIntent(this);
Steve McKaydf5add42016-01-28 12:02:57 -080077
78 // Forward any flags from the original intent.
79 intent.setFlags(getIntent().getFlags());
Steve McKay6c8bfb72015-10-05 12:32:46 -070080 if (DEBUG) Log.d(TAG, "Starting new task > " + intent.getData());
Steve McKayf2c8b0d2015-09-23 15:44:24 -070081 startActivity(intent);
82 }
83
Steve McKay6c8bfb72015-10-05 12:32:46 -070084 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 McKayf2c8b0d2015-09-23 15:44:24 -070088 }
89
Steve McKaydf5add42016-01-28 12:02:57 -080090 static final Intent createLaunchIntent(Context context) {
Steve McKayf2c8b0d2015-09-23 15:44:24 -070091 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 McKay6c8bfb72015-10-05 12:32:46 -0700104 boolean result = uri != null && LAUNCH_CONTROL_AUTHORITY.equals(uri.getAuthority());
105 return result;
Steve McKayf2c8b0d2015-09-23 15:44:24 -0700106 }
107}