OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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.chrome.browser.prerender; |
| 6 |
| 7 import android.app.Activity; |
| 8 import android.content.ComponentName; |
| 9 import android.content.Context; |
| 10 import android.content.Intent; |
| 11 import android.content.ServiceConnection; |
| 12 import android.net.Uri; |
| 13 import android.os.Bundle; |
| 14 import android.os.IBinder; |
| 15 import android.os.Message; |
| 16 import android.os.Messenger; |
| 17 import android.os.RemoteException; |
| 18 import android.view.View; |
| 19 import android.view.View.OnClickListener; |
| 20 import android.widget.EditText; |
| 21 |
| 22 import com.google.android.apps.chrome.R; |
| 23 |
| 24 /** |
| 25 * An activity for testing the prerender API. This can test whether |
| 26 * ChromePrerenderService warms up Chrome and prerender a given url |
| 27 * successfully. |
| 28 */ |
| 29 public class PrerenderAPITestActivity extends Activity implements OnClickListene
r { |
| 30 private static final String DEFAULT_URL = "http://www.nytimes.com"; |
| 31 static final int MSG_PRERENDER_URL = 1; |
| 32 static final String KEY_PREPRENDERED_URL = "url_to_preprender"; |
| 33 private String mUrl; |
| 34 |
| 35 Messenger mService = null; |
| 36 |
| 37 private final ServiceConnection mConnection = new ServiceConnection() { |
| 38 @Override |
| 39 public void onServiceConnected(ComponentName className, IBinder service)
{ |
| 40 mService = new Messenger(service); |
| 41 } |
| 42 |
| 43 @Override |
| 44 public void onServiceDisconnected(ComponentName className) { |
| 45 mService = null; |
| 46 } |
| 47 }; |
| 48 |
| 49 @Override |
| 50 protected void onCreate(Bundle savedInstanceState) { |
| 51 super.onCreate(savedInstanceState); |
| 52 setContentView(R.layout.prerender_test_main); |
| 53 findViewById(R.id.preload_button).setOnClickListener(this); |
| 54 findViewById(R.id.load_button).setOnClickListener(this); |
| 55 ((EditText) findViewById(R.id.url_to_load)).setText(DEFAULT_URL); |
| 56 |
| 57 mUrl = DEFAULT_URL; |
| 58 } |
| 59 |
| 60 @Override |
| 61 protected void onResume() { |
| 62 super.onResume(); |
| 63 Intent intent = new Intent(); |
| 64 intent.setClassName(getApplicationContext().getPackageName(), |
| 65 ChromePrerenderService.class.getName()); |
| 66 bindService(intent, mConnection, Context.BIND_AUTO_CREATE); |
| 67 } |
| 68 |
| 69 @Override |
| 70 protected void onDestroy() { |
| 71 super.onDestroy(); |
| 72 if (mService != null) unbindService(mConnection); |
| 73 } |
| 74 |
| 75 /** |
| 76 * Prerender a url using ChromePrerenderService. |
| 77 * @param url The url to be prerendered. |
| 78 */ |
| 79 public void prerenderUrl(String url) { |
| 80 if (mService == null) return; |
| 81 Message msg = Message.obtain( |
| 82 null, ChromePrerenderService.MSG_PRERENDER_URL, 0, 0); |
| 83 Bundle data = new Bundle(); |
| 84 data.putString(ChromePrerenderService.KEY_PRERENDERED_URL, url); |
| 85 msg.setData(data); |
| 86 try { |
| 87 mService.send(msg); |
| 88 } catch (RemoteException e) { |
| 89 e.printStackTrace(); |
| 90 } |
| 91 } |
| 92 |
| 93 @Override |
| 94 public void onClick(View view) { |
| 95 mUrl = ((EditText) findViewById(R.id.url_to_load)).getText().toString(); |
| 96 if (view.getId() == R.id.preload_button) { |
| 97 prerenderUrl(mUrl); |
| 98 } else if (view.getId() == R.id.load_button) { |
| 99 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(mUrl)); |
| 100 intent.setPackage(getApplicationContext().getPackageName()); |
| 101 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 102 startActivity(intent); |
| 103 } |
| 104 } |
| 105 |
| 106 } |
OLD | NEW |