OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.blimp; | |
6 | |
7 import android.app.Activity; | |
8 import android.content.Context; | |
9 import android.content.Intent; | |
10 import android.os.Bundle; | |
11 import android.os.Process; | |
12 | |
13 /** | |
14 * Kills and (optionally) restarts the main Blimp process, then immediately kill
s itself. | |
15 * | |
16 * Starting this Activity requires passing in the process ID (the Intent should
have the value of | |
17 * Process#myPid() as an extra). | |
18 * | |
19 * This Activity runs on a separate process from the main Blimp browser and cann
ot see the main | |
20 * process' Activities. It is an workaround for crbug.com/515919 which doesn't
use AlarmManager. | |
21 */ | |
22 public class BrowserRestartActivity extends Activity { | |
23 private static final String EXTRA_MAIN_PID = | |
24 "org.chromium.blimp.browser.BrowserRestartActivity.main_pid"; | |
25 private static final String EXTRA_RESTART = | |
26 "org.chromium.blimp.browser.BrowserRestartActivity.restart"; | |
27 | |
28 private static final String TAG = "BrowserRestartActivity"; | |
29 | |
30 @Override | |
31 public void onCreate(Bundle savedInstanceState) { | |
32 super.onCreate(savedInstanceState); | |
33 destroyProcess(getIntent()); | |
34 } | |
35 | |
36 @Override | |
37 public void onNewIntent(Intent intent) { | |
38 destroyProcess(getIntent()); | |
39 } | |
40 | |
41 private void destroyProcess(Intent intent) { | |
42 // Kill the main Blimp process. | |
43 int mainBrowserPid = intent.getIntExtra(BrowserRestartActivity.EXTRA_MAI
N_PID, -1); | |
44 assert mainBrowserPid != -1; | |
45 Process.killProcess(mainBrowserPid); | |
46 | |
47 // Fire an Intent to restart Blimp. | |
48 boolean restart = intent.getBooleanExtra(BrowserRestartActivity.EXTRA_RE
START, false); | |
49 if (restart) { | |
50 Intent restartIntent = new Intent(Intent.ACTION_MAIN); | |
51 restartIntent.setPackage(getPackageName()); | |
52 restartIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
53 startActivity(restartIntent); | |
54 } | |
55 | |
56 // Kill this process. | |
57 finish(); | |
58 Process.killProcess(Process.myPid()); | |
59 } | |
60 | |
61 /** | |
62 * Helper method to create an Intent to restart the browser. | |
63 * @param context The current android context | |
64 * @return Intent to be fired | |
65 */ | |
66 public static Intent createRestartIntent(Context context) { | |
67 Intent intent = new Intent(); | |
68 intent.setClassName(context.getPackageName(), BrowserRestartActivity.cla
ss.getName()); | |
69 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
70 intent.putExtra(BrowserRestartActivity.EXTRA_MAIN_PID, Process.myPid()); | |
71 intent.putExtra(BrowserRestartActivity.EXTRA_RESTART, true); | |
72 return intent; | |
73 } | |
74 } | |
OLD | NEW |