018731ca68289230ba1f341715054564497bf737
[JavaForFun] /
1 /**
2  * Copyright 2014 Gustavo Martin Morcuende
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 package name.gumartinm.weather.information.activity;
17
18 import android.app.ActionBar;
19 import android.app.Activity;
20 import android.os.Bundle;
21 import android.util.Log;
22 import android.webkit.WebView;
23
24 import com.google.android.gms.common.GooglePlayServicesUtil;
25 import name.gumartinm.weather.information.R;
26
27 import java.io.BufferedReader;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.InputStreamReader;
31 import java.io.UnsupportedEncodingException;
32 import java.nio.charset.Charset;
33
34 public class LicensesActivity extends Activity {
35     private static final String TAG = "LicensesActivity";
36     private WebView mWebView;
37
38
39     @Override
40     public void onCreate(final Bundle savedInstanceState) {
41         super.onCreate(savedInstanceState);
42         setContentView(R.layout.weather_licenses);
43
44         mWebView = (WebView) this.findViewById(R.id.weather_licenses);
45     }
46
47     @Override
48     public void onResume() {
49         super.onResume();
50
51         final ActionBar actionBar = this.getActionBar();
52         actionBar.setTitle(this.getString(R.string.weather_licenses_title));
53
54         final String googlePlayServices = GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(this.getApplicationContext());
55         try {
56             final StringBuilder stringBuilder = this.loadData();
57             stringBuilder.append(googlePlayServices).append("</pre>").append("</body>").append("</html>");
58             mWebView.loadDataWithBaseURL(null, stringBuilder.toString(), "text/html", "UTF-8", null);
59         } catch (final UnsupportedEncodingException e) {
60             Log.e(TAG, "Load data error", e);
61         } catch (final IOException e) {
62             Log.e(TAG, "Load data error", e);
63         }
64     }
65
66     private StringBuilder loadData() throws UnsupportedEncodingException, IOException {
67         final InputStream inputStream = this.getResources().openRawResource(R.raw.licenses);
68         try {
69             final InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
70             try {
71                 final BufferedReader reader = new BufferedReader(inputStreamReader);
72                 try {
73                     final StringBuilder stringBuilder = new StringBuilder();
74                     String line;
75                     while ((line = reader.readLine()) != null) {
76                         stringBuilder.append(line);
77                         stringBuilder.append("\n");
78                     }
79                     return stringBuilder;
80                 } finally {
81                     reader.close();
82                 }
83             } finally {
84                 inputStreamReader.close();
85             }
86         } finally {
87             inputStream.close();
88         }
89     }
90 }