• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 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.mojo_shell_apk;
6 
7 import android.app.Activity;
8 import android.app.AlertDialog;
9 import android.content.DialogInterface;
10 import android.content.Intent;
11 import android.os.Bundle;
12 import android.util.Log;
13 import android.widget.EditText;
14 
15 import org.chromium.base.library_loader.LibraryLoader;
16 import org.chromium.base.library_loader.ProcessInitException;
17 
18 /**
19  * Activity for managing the Mojo Shell.
20  */
21 public class MojoShellActivity extends Activity {
22     private static final String TAG = "MojoShellActivity";
23 
24     @Override
onCreate(final Bundle savedInstanceState)25     protected void onCreate(final Bundle savedInstanceState) {
26         super.onCreate(savedInstanceState);
27 
28         try {
29             LibraryLoader.ensureInitialized();
30         } catch (ProcessInitException e) {
31             Log.e(TAG, "libmojo_shell initialization failed.", e);
32             finish();
33             return;
34         }
35 
36         MojoMain.ensureInitialized(this);
37 
38         String appUrl = getUrlFromIntent(getIntent());
39         if (appUrl == null) {
40             Log.i(TAG, "No URL provided via intent, prompting user...");
41             AlertDialog.Builder alert = new AlertDialog.Builder(this);
42             alert.setTitle("Enter a URL");
43             alert.setMessage("Enter a URL");
44             final EditText input = new EditText(this);
45             alert.setView(input);
46             alert.setPositiveButton("Load", new DialogInterface.OnClickListener() {
47                 @Override
48                 public void onClick(DialogInterface dialog, int button) {
49                     String url = input.getText().toString();
50                     startWithURL(url);
51                 }
52             });
53             alert.show();
54         } else {
55             startWithURL(appUrl);
56         }
57     }
58 
getUrlFromIntent(Intent intent)59     private static String getUrlFromIntent(Intent intent) {
60         return intent != null ? intent.getDataString() : null;
61     }
62 
startWithURL(String url)63     private void startWithURL(String url) {
64         MojoMain.start(url);
65         Log.i(TAG, "Mojo started: " + url);
66     }
67 }
68