1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settings;
18 
19 import android.app.Activity;
20 import android.content.ActivityNotFoundException;
21 import android.content.Intent;
22 import android.content.res.Resources;
23 import android.net.Uri;
24 import android.os.Bundle;
25 import android.os.SystemProperties;
26 import android.text.TextUtils;
27 import android.util.Log;
28 import android.widget.Toast;
29 
30 import java.io.File;
31 
32 /**
33  * The "dialog" that shows from "Manual" in the Settings app.
34  */
35 public class ManualDisplayActivity extends Activity {
36     private static final String TAG = "SettingsManualActivity";
37 
38     private static final String DEFAULT_MANUAL_PATH = "/system/etc/MANUAL.html.gz";
39     private static final String PROPERTY_MANUAL_PATH = "ro.config.manual_path";
40 
41     @Override
onCreate(Bundle savedInstanceState)42     protected void onCreate(Bundle savedInstanceState) {
43         super.onCreate(savedInstanceState);
44         Resources resources = getResources();
45 
46         if (!resources.getBoolean(R.bool.config_show_manual)) {
47             finish();   // No manual to display for this device
48         }
49 
50         final String path = SystemProperties.get(PROPERTY_MANUAL_PATH, DEFAULT_MANUAL_PATH);
51         if (TextUtils.isEmpty(path)) {
52             Log.e(TAG, "The system property for the manual is empty");
53             showErrorAndFinish();
54             return;
55         }
56 
57         final File file = new File(path);
58         if (!file.exists() || file.length() == 0) {
59             Log.e(TAG, "Manual file " + path + " does not exist");
60             showErrorAndFinish();
61             return;
62         }
63 
64         final Intent intent = new Intent(Intent.ACTION_VIEW);
65         intent.setDataAndType(Uri.fromFile(file), "text/html");
66 
67         intent.putExtra(Intent.EXTRA_TITLE, getString(R.string.settings_manual_activity_title));
68         intent.addCategory(Intent.CATEGORY_DEFAULT);
69         intent.setPackage("com.android.htmlviewer");
70 
71         try {
72             startActivity(intent);
73             finish();
74         } catch (ActivityNotFoundException e) {
75             Log.e(TAG, "Failed to find viewer", e);
76             showErrorAndFinish();
77         }
78     }
79 
showErrorAndFinish()80     private void showErrorAndFinish() {
81         Toast.makeText(this, R.string.settings_manual_activity_unavailable, Toast.LENGTH_LONG)
82                 .show();
83         finish();
84     }
85 }
86