Project

General

Profile

Download (4.56 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / tutorial / TutorialWebView.java @ 2971588c

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.tutorial;
21

    
22
import android.annotation.SuppressLint;
23
import android.content.Context;
24
import android.content.res.Resources;
25
import android.webkit.WebSettings;
26
import android.webkit.WebView;
27

    
28
import org.distorted.main.R;
29

    
30
import java.io.BufferedReader;
31
import java.io.InputStream;
32
import java.io.InputStreamReader;
33

    
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35

    
36
public class TutorialWebView
37
{
38
    private String  mUrl;
39
    private Context mContext;
40
    private WebView mWebView;
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

    
44
    @SuppressLint("SetJavaScriptEnabled")
45
    public TutorialWebView(Context context, WebView webview)
46
      {
47
      mWebView = webview;
48
      mContext = context;
49
      mWebView.setBackgroundColor(0);
50
      mWebView.getSettings().setJavaScriptEnabled(true);
51
      }
52

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

    
55
    public void load(String url)
56
      {
57
      mUrl = url;
58
      String data = readFromfile(mContext);
59
      data = data.replace("%1", url);
60

    
61
android.util.Log.e("webview", "data= "+data);
62

    
63
mWebView.getSettings().setAppCacheMaxSize( 10 * 1024 * 1024 ); // 10MB
64
mWebView.getSettings().setAppCachePath(mContext.getCacheDir().getAbsolutePath() );
65
mWebView.getSettings().setAllowFileAccess( true );
66
mWebView.getSettings().setAppCacheEnabled( true );
67
mWebView.getSettings().setCacheMode( WebSettings.LOAD_DEFAULT );
68

    
69
      mWebView.loadData(data, "text/html", "UTF-8");
70
      }
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

    
74
    public String readFromfile(Context context)
75
      {
76
      StringBuilder returnString = new StringBuilder();
77
      InputStream fIn       = null;
78
      InputStreamReader isr = null;
79
      BufferedReader input  = null;
80

    
81
      try
82
        {
83
        Resources res = context.getResources();
84
        fIn = res.openRawResource(R.raw.webvideo);
85
        isr = new InputStreamReader(fIn);
86
        input = new BufferedReader(isr);
87
        String line;
88

    
89
        while ((line = input.readLine()) != null)
90
          {
91
          returnString.append(line);
92
          }
93
        }
94
      catch (Exception e)
95
        {
96
        e.getMessage();
97
        }
98
      finally
99
        {
100
        try
101
          {
102
          if (isr   != null) isr.close();
103
          if (fIn   != null) fIn.close();
104
          if (input != null) input.close();
105
          }
106
        catch (Exception e2)
107
          {
108
          e2.getMessage();
109
          }
110
        }
111

    
112
      return returnString.toString();
113
      }
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116

    
117
    public void reload()
118
      {
119
      if (mUrl!=null)
120
        {
121
        load(mUrl);
122
        }
123
      }
124
}
(6-6/6)