1/*
2 * Copyright (C) 2014 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
17package com.android.tv.settings.about;
18
19import com.android.tv.settings.R;
20
21import android.app.Activity;
22import android.os.AsyncTask;
23import android.os.Bundle;
24import android.os.SystemProperties;
25import android.text.TextUtils;
26import android.util.Log;
27import android.view.View;
28import android.webkit.WebView;
29import android.widget.Toast;
30
31import java.io.FileInputStream;
32import java.io.FileNotFoundException;
33import java.io.FileReader;
34import java.io.IOException;
35import java.io.InputStreamReader;
36import java.util.zip.GZIPInputStream;
37
38/**
39 * Displays open source NOTICE files.
40 */
41public class LicenseActivity extends Activity {
42
43    private static final String TAG = "LicenseActivity";
44    private static final boolean LOGV = false;
45
46    private static final String DEFAULT_LICENSE_PATH = "/system/etc/NOTICE.html.gz";
47    private static final String PROPERTY_LICENSE_PATH = "ro.config.license_path";
48
49    private WebView mWebView;
50
51    @Override
52    protected void onCreate(Bundle savedInstanceState) {
53        super.onCreate(savedInstanceState);
54        setContentView(R.layout.license_activity);
55        mWebView = (WebView) findViewById(R.id.license);
56
57        String fileName = SystemProperties.get(PROPERTY_LICENSE_PATH, DEFAULT_LICENSE_PATH);
58        if (TextUtils.isEmpty(fileName)) {
59            Log.e(TAG, "The system property for the license file is empty.");
60            showErrorAndFinish();
61            return;
62        }
63
64        new LicenseFileLoader().execute(fileName);
65    }
66
67    private class LicenseFileLoader extends AsyncTask<String, Void, String> {
68
69        public static final int STATUS_OK = 0;
70        public static final int STATUS_NOT_FOUND = 1;
71        public static final int STATUS_READ_ERROR = 2;
72        public static final int STATUS_EMPTY_FILE = 3;
73
74        @Override
75        protected void onPreExecute() {
76            // TODO: implement loading text or graphic here.
77            mWebView.setVisibility(View.GONE);
78        }
79
80        @Override
81        protected String doInBackground(String... params) {
82            int status = STATUS_OK;
83            String fileName = params[0];
84
85            InputStreamReader inputReader = null;
86            StringBuilder data = new StringBuilder(2048);
87            try {
88                char[] tmp = new char[2048];
89                int numRead;
90                if (fileName.endsWith(".gz")) {
91                    inputReader = new InputStreamReader(
92                        new GZIPInputStream(new FileInputStream(fileName)));
93                } else {
94                    inputReader = new FileReader(fileName);
95                }
96
97                while ((numRead = inputReader.read(tmp)) >= 0) {
98                    data.append(tmp, 0, numRead);
99                }
100            } catch (FileNotFoundException e) {
101                Log.e(TAG, "License HTML file not found at " + fileName, e);
102                status = STATUS_NOT_FOUND;
103            } catch (IOException e) {
104                Log.e(TAG, "Error reading license HTML file at " + fileName, e);
105                status = STATUS_READ_ERROR;
106            } finally {
107                try {
108                    if (inputReader != null) {
109                        inputReader.close();
110                    }
111                } catch (IOException e) {
112                }
113            }
114
115            if ((status == STATUS_OK) && TextUtils.isEmpty(data)) {
116                Log.e(TAG, "License HTML is empty (from " + fileName + ")");
117                status = STATUS_EMPTY_FILE;
118            }
119            return data.toString();
120        }
121
122        @Override
123        protected void onPostExecute(String result) {
124            showDataInPage(result);
125        }
126    }
127
128    private void showDataInPage(String data) {
129        // TODO: implement hiding loading text or graphic here.
130        mWebView.setVisibility(View.VISIBLE);
131        mWebView.loadDataWithBaseURL(null, data, "text/html", "utf-8", null);
132    }
133
134    private void showErrorAndFinish() {
135        Toast.makeText(this, R.string.about_license_activity_unavailable, Toast.LENGTH_LONG)
136                .show();
137        finish();
138    }
139}
140