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
.support
.v7
.app
.ActionBarActivity
;
21 import android
.text
.TextUtils
;
22 import android
.util
.Log
;
23 import android
.view
.Menu
;
24 import android
.view
.MenuItem
;
25 import android
.view
.View
;
26 import android
.webkit
.WebView
;
27 import android
.webkit
.WebViewClient
;
29 import org
.chromium
.ui
.UiUtils
;
32 * The Activity for showing the Help screen.
34 public class HelpActivity
extends ActionBarActivity
{
35 private static final String PLAY_STORE_URL
= "market://details?id=";
36 private static final String CREDITS_URL
= "file:///android_res/raw/credits.html";
38 private static final String FEEDBACK_PACKAGE
= "com.google.android.gms";
40 private static final String FEEDBACK_CLASS
=
41 "com.google.android.gms.feedback.LegacyBugReportService";
44 * Maximum dimension for the screenshot to be sent to the Send Feedback handler. This size
45 * ensures the size of bitmap < 1MB, which is a requirement of the handler.
47 private static final int MAX_FEEDBACK_SCREENSHOT_DIMENSION
= 600;
50 * This global variable is used for passing the screenshot from the originating Activity to the
51 * HelpActivity. There seems to be no better way of doing this.
53 private static Bitmap sScreenshot
;
55 /** WebView used to display help content and credits. */
56 private WebView mWebView
;
58 /** Constant used to send the feedback parcel to the system feedback service. */
59 private static final int SEND_FEEDBACK_INFO
= Binder
.FIRST_CALL_TRANSACTION
;
61 /** Launches an external web browser or application. */
62 private void openUrl(String url
) {
63 Uri uri
= Uri
.parse(url
);
64 Intent intent
= new Intent(Intent
.ACTION_VIEW
, uri
);
66 // Verify that the device can launch an application for this intent, otherwise
67 // startActivity() may crash the application.
68 if (intent
.resolveActivity(getPackageManager()) != null) {
69 startActivity(intent
);
73 private void sendFeedback() {
74 Intent intent
= new Intent(Intent
.ACTION_BUG_REPORT
);
75 intent
.setComponent(new ComponentName(FEEDBACK_PACKAGE
, FEEDBACK_CLASS
));
76 if (getPackageManager().resolveService(intent
, 0) == null) {
77 Log
.e("help", "Unable to resolve Feedback service.");
81 ServiceConnection conn
= new ServiceConnection() {
83 public void onServiceConnected(ComponentName name
, IBinder service
) {
85 Parcel parcel
= Parcel
.obtain();
86 if (sScreenshot
!= null) {
87 sScreenshot
.writeToParcel(parcel
, 0);
89 service
.transact(SEND_FEEDBACK_INFO
, parcel
, null, 0);
91 } catch (RemoteException ex
) {
92 Log
.e("help", "Unexpected error sending feedback: ", ex
);
97 public void onServiceDisconnected(ComponentName name
) {}
100 bindService(intent
, conn
, BIND_AUTO_CREATE
);
103 /** Launches the Help activity. */
104 public static void launch(Activity activity
, String helpUrl
) {
105 View rootView
= activity
.getWindow().getDecorView().getRootView();
106 sScreenshot
= UiUtils
.generateScaledScreenshot(rootView
, MAX_FEEDBACK_SCREENSHOT_DIMENSION
,
107 Bitmap
.Config
.ARGB_8888
);
109 Intent intent
= new Intent(activity
, HelpActivity
.class);
110 intent
.setData(Uri
.parse(helpUrl
));
111 activity
.startActivity(intent
);
115 public void onCreate(Bundle savedInstanceState
) {
116 super.onCreate(savedInstanceState
);
118 mWebView
= new WebView(this);
119 setContentView(mWebView
);
121 getSupportActionBar().setTitle(getString(R
.string
.actionbar_help_title
));
123 CharSequence appName
= getTitle();
124 CharSequence versionName
= null;
126 PackageInfo info
= getPackageManager().getPackageInfo(getPackageName(), 0);
127 versionName
= info
.versionName
;
128 } catch (PackageManager
.NameNotFoundException ex
) {
129 throw new RuntimeException("Unable to get version: " + ex
);
132 CharSequence subtitle
= TextUtils
.concat(appName
, " ", versionName
);
133 getSupportActionBar().setSubtitle(subtitle
);
135 String initialUrl
= getIntent().getDataString();
136 final String initialHost
= Uri
.parse(initialUrl
).getHost();
138 mWebView
.getSettings().setJavaScriptEnabled(true);
139 mWebView
.setWebViewClient(new WebViewClient() {
141 public boolean shouldOverrideUrlLoading(WebView view
, String url
) {
142 // Make sure any links to other websites open up in an external browser.
143 String host
= Uri
.parse(url
).getHost();
145 // Note that |host| might be null, so allow for this in the test for equality.
146 if (initialHost
.equals(host
)) {
153 mWebView
.loadUrl(initialUrl
);
157 public boolean onCreateOptionsMenu(Menu menu
) {
158 getMenuInflater().inflate(R
.menu
.help_actionbar
, menu
);
159 return super.onCreateOptionsMenu(menu
);
163 public boolean onOptionsItemSelected(MenuItem item
) {
164 int id
= item
.getItemId();
165 if (id
== R
.id
.actionbar_feedback
) {
169 if (id
== R
.id
.actionbar_play_store
) {
170 openUrl(PLAY_STORE_URL
+ getPackageName());
173 if (id
== R
.id
.actionbar_credits
) {
174 mWebView
.loadUrl(CREDITS_URL
);
177 return super.onOptionsItemSelected(item
);