HelpActivity.java revision 0529e5d033099cbfc42635f6f6183833b09dff6e
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.
4
5package org.chromium.chromoting;
6
7import android.app.Activity;
8import android.content.ComponentName;
9import android.content.Intent;
10import android.content.ServiceConnection;
11import android.content.pm.PackageInfo;
12import android.content.pm.PackageManager;
13import android.graphics.Bitmap;
14import android.net.Uri;
15import android.os.Binder;
16import android.os.Bundle;
17import android.os.IBinder;
18import android.os.Parcel;
19import android.os.RemoteException;
20import android.text.TextUtils;
21import android.util.Log;
22import android.view.Menu;
23import android.view.MenuItem;
24import android.view.View;
25import android.webkit.WebView;
26import android.webkit.WebViewClient;
27
28import org.chromium.ui.UiUtils;
29
30/**
31 * The Activity for showing the Help screen.
32 */
33public class HelpActivity extends Activity {
34    private static final String PLAY_STORE_URL = "market://details?id=";
35
36    /**
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.
39     */
40    private static final int MAX_FEEDBACK_SCREENSHOT_DIMENSION = 600;
41
42    /**
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.
45     */
46    private static Bitmap mScreenshot;
47
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;
50
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);
55
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);
60        }
61    }
62
63    private void sendFeedback() {
64        Intent intent = new Intent(Intent.ACTION_BUG_REPORT);
65        ServiceConnection conn = new ServiceConnection() {
66            @Override
67            public void onServiceConnected(ComponentName name, IBinder service) {
68                try {
69                    Parcel parcel = Parcel.obtain();
70                    if (mScreenshot != null) {
71                        mScreenshot.writeToParcel(parcel, 0);
72                    }
73                    service.transact(SEND_FEEDBACK_INFO, parcel, null, 0);
74                    parcel.recycle();
75                } catch (RemoteException ex) {
76                    Log.e("help", "Unexpected error sending feedback: ", ex);
77                }
78            }
79
80            @Override
81            public void onServiceDisconnected(ComponentName name) {}
82        };
83
84        bindService(intent, conn, BIND_AUTO_CREATE);
85    }
86
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);
92
93        Intent intent = new Intent(activity, HelpActivity.class);
94        intent.setData(Uri.parse(helpUrl));
95        activity.startActivity(intent);
96    }
97
98    @Override
99    public void onCreate(Bundle savedInstanceState) {
100        super.onCreate(savedInstanceState);
101
102        WebView webView = new WebView(this);
103        setContentView(webView);
104
105        getActionBar().setTitle(getString(R.string.actionbar_help_title));
106
107        CharSequence appName = getTitle();
108        CharSequence versionName = null;
109        try {
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);
114        }
115
116        CharSequence subtitle = TextUtils.concat(appName, " ", versionName);
117        getActionBar().setSubtitle(subtitle);
118
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);
125    }
126
127    @Override
128    public boolean onCreateOptionsMenu(Menu menu) {
129        getMenuInflater().inflate(R.menu.help_actionbar, menu);
130        return super.onCreateOptionsMenu(menu);
131    }
132
133    @Override
134    public boolean onOptionsItemSelected(MenuItem item) {
135        int id = item.getItemId();
136        if (id == R.id.actionbar_feedback) {
137            sendFeedback();
138            return true;
139        }
140        if (id == R.id.actionbar_play_store) {
141            openUrl(PLAY_STORE_URL + getPackageName());
142            return true;
143        }
144        return super.onOptionsItemSelected(item);
145    }
146}
147