summaryrefslogtreecommitdiff
path: root/app/src/main/java/de/danoeh/antennapod/activity/AboutActivity.java
blob: b3418e7b23ef3f9d8c3b7fa860cabd612a92b4cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package de.danoeh.antennapod.activity;

import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import rx.Single;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Displays the 'about' screen
 */
public class AboutActivity extends AppCompatActivity {

    private static final String TAG = AboutActivity.class.getSimpleName();

    private WebView webView;
    private LinearLayout webViewContainer;
    private Subscription subscription;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(UserPreferences.getTheme());
        super.onCreate(savedInstanceState);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        setContentView(R.layout.about);
        webViewContainer = (LinearLayout) findViewById(R.id.webViewContainer);
        webView = (WebView) findViewById(R.id.webViewAbout);
        webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        if (UserPreferences.getTheme() == R.style.Theme_AntennaPod_Dark) {
            if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
                webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
            }
            webView.setBackgroundColor(Color.TRANSPARENT);
        }
        webView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (!url.startsWith("http")) {
                    url = url.replace("file:///android_asset/", "");
                    loadAsset(url);
                    return true;
                }
                return false;
            }

        });
        loadAsset("about.html");
    }

    private void loadAsset(String filename) {
        subscription = Single.create(subscriber -> {
            InputStream input = null;
            try {
                TypedArray res = AboutActivity.this.getTheme().obtainStyledAttributes(
                        new int[] { android.R.attr.textColorPrimary });
                int colorResource = res.getColor(0, 0);
                String colorString = String.format("#%06X", 0xFFFFFF & colorResource);
                res.recycle();
                input = getAssets().open(filename);
                String webViewData = IOUtils.toString(input, Charset.defaultCharset());
                if (!webViewData.startsWith("<!DOCTYPE html>")) {
                    webViewData = webViewData.replace("%", "&#37;");
                    webViewData =
                            "<!DOCTYPE html>" +
                            "<html>" +
                            "<head>" +
                            "    <meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\">" +
                            "    <style type=\"text/css\">" +
                            "        @font-face {" +
                            "        font-family: 'Roboto-Light';" +
                            "           src: url('file:///android_asset/Roboto-Light.ttf');" +
                            "        }" +
                            "        * {" +
                            "           color: %s;" +
                            "           font-family: roboto-Light;" +
                            "           font-size: 8pt;" +
                            "        }" +
                            "    </style>" +
                            "</head><body><p>" + webViewData + "</p></body></html>";
                    webViewData = webViewData.replace("\n", "<br/>");
                }
                webViewData = String.format(webViewData, colorString);
                subscriber.onSuccess(webViewData);
            } catch (IOException e) {
                Log.e(TAG, Log.getStackTraceString(e));
                subscriber.onError(e);
            } finally {
                IOUtils.closeQuietly(input);
            }
        })
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        webViewData ->
                                webView.loadDataWithBaseURL("file:///android_asset/", webViewData.toString(), "text/html", "utf-8", "file:///android_asset/" + webViewData.toString()),
                        error -> Log.e(TAG, Log.getStackTraceString(error))
                );
    }

    @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
            return true;
        } else {
            return super.onOptionsItemSelected(item);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(subscription != null) {
            subscription.unsubscribe();
        }
        if (webViewContainer != null && webView != null) {
            webViewContainer.removeAllViews();
            webView.destroy();
        }
    }
}