1 // Copyright 2014 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.
5 package org.chromium.chromoting;
7 import android.app.Activity;
8 import android.content.ComponentName;
9 import android.content.Intent;
10 import android.content.ServiceConnection;
11 import android.content.pm.PackageInfo;
12 import android.content.pm.PackageManager;
13 import android.graphics.Bitmap;
14 import android.net.Uri;
15 import android.os.Binder;
16 import android.os.Bundle;
17 import android.os.IBinder;
18 import android.os.Parcel;
19 import android.os.RemoteException;
20 import android.text.TextUtils;
21 import android.util.Log;
22 import android.view.Menu;
23 import android.view.MenuItem;
24 import android.view.View;
25 import android.webkit.WebView;
26 import android.webkit.WebViewClient;
28 import org.chromium.ui.UiUtils;
31 * The Activity for showing the Help screen.
33 public class HelpActivity extends Activity {
34 private static final String PLAY_STORE_URL = "market://details?id=";
37 * Maximum dimension for the screenshot to be sent to the Send Feedback handler. This size
38 * ensures the size of bitmap < 1MB, which is a requirement of the handler.
40 private static final int MAX_FEEDBACK_SCREENSHOT_DIMENSION = 600;
43 * This global variable is used for passing the screenshot from the originating Activity to the
44 * HelpActivity. There seems to be no better way of doing this.
46 private static Bitmap mScreenshot;
48 /** Constant used to send the feedback parcel to the system feedback service. */
49 private static final int SEND_FEEDBACK_INFO = Binder.FIRST_CALL_TRANSACTION;
51 /** Launches an external web browser or application. */
52 private void openUrl(String url) {
53 Uri uri = Uri.parse(url);
54 Intent intent = new Intent(Intent.ACTION_VIEW, uri);
56 // Verify that the device can launch an application for this intent, otherwise
57 // startActivity() may crash the application.
58 if (intent.resolveActivity(getPackageManager()) != null) {
59 startActivity(intent);
63 private void sendFeedback() {
64 Intent intent = new Intent(Intent.ACTION_BUG_REPORT);
65 ServiceConnection conn = new ServiceConnection() {
67 public void onServiceConnected(ComponentName name, IBinder service) {
69 Parcel parcel = Parcel.obtain();
70 if (mScreenshot != null) {
71 mScreenshot.writeToParcel(parcel, 0);
73 service.transact(SEND_FEEDBACK_INFO, parcel, null, 0);
75 } catch (RemoteException ex) {
76 Log.e("help", "Unexpected error sending feedback: ", ex);
81 public void onServiceDisconnected(ComponentName name) {}
84 bindService(intent, conn, BIND_AUTO_CREATE);
87 /** Launches the Help activity. */
88 public static void launch(Activity activity, String helpUrl) {
89 View rootView = activity.getWindow().getDecorView().getRootView();
90 mScreenshot = UiUtils.generateScaledScreenshot(rootView, MAX_FEEDBACK_SCREENSHOT_DIMENSION,
91 Bitmap.Config.ARGB_8888);
93 Intent intent = new Intent(activity, HelpActivity.class);
94 intent.setData(Uri.parse(helpUrl));
95 activity.startActivity(intent);
99 public void onCreate(Bundle savedInstanceState) {
100 super.onCreate(savedInstanceState);
102 WebView webView = new WebView(this);
103 setContentView(webView);
105 getActionBar().setTitle(getString(R.string.actionbar_help_title));
107 CharSequence appName = getTitle();
108 CharSequence versionName = null;
110 PackageInfo info = getPackageManager().getPackageInfo(getPackageName(), 0);
111 versionName = info.versionName;
112 } catch (PackageManager.NameNotFoundException ex) {
113 throw new RuntimeException("Unable to get version: " + ex);
116 CharSequence subtitle = TextUtils.concat(appName, " ", versionName);
117 getActionBar().setSubtitle(subtitle);
119 // This line ensures the WebView remains embedded in this activity and doesn't launch an
120 // external Chrome browser.
121 webView.setWebViewClient(new WebViewClient());
122 webView.getSettings().setJavaScriptEnabled(true);
123 String url = getIntent().getDataString();
124 webView.loadUrl(url);
128 public boolean onCreateOptionsMenu(Menu menu) {
129 getMenuInflater().inflate(R.menu.help_actionbar, menu);
130 return super.onCreateOptionsMenu(menu);
134 public boolean onOptionsItemSelected(MenuItem item) {
135 int id = item.getItemId();
136 if (id == R.id.actionbar_feedback) {
140 if (id == R.id.actionbar_play_store) {
141 openUrl(PLAY_STORE_URL + getPackageName());
144 return super.onOptionsItemSelected(item);