|
12 | 12 | import android.webkit.WebViewClient; |
13 | 13 | import android.widget.ProgressBar; |
14 | 14 | import androidx.appcompat.app.AppCompatActivity; |
| 15 | +import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; |
15 | 16 | import com.google.android.material.appbar.MaterialToolbar; |
16 | 17 | import java.io.BufferedReader; |
17 | 18 | import java.io.InputStream; |
18 | 19 | import java.io.InputStreamReader; |
19 | 20 | import org.xedox.utils.BaseActivity; |
| 21 | +import org.xedox.utils.dialog.ErrorDialog; |
20 | 22 | import org.xedox.utils.view.WebViewX; |
21 | 23 |
|
22 | 24 | public class PreviewActivity extends BaseActivity { |
23 | 25 |
|
24 | 26 | private MaterialToolbar toolbar; |
25 | 27 | private WebViewX webView; |
26 | 28 | private ProgressBar progress; |
| 29 | + private SwipeRefreshLayout swipeRefresh; |
27 | 30 |
|
28 | 31 | @Override |
29 | 32 | protected void onCreate(Bundle savedInstanceState) { |
30 | 33 | super.onCreate(savedInstanceState); |
31 | | - setContentView(R.layout.activity_preview); |
32 | | - toolbar = findViewById(R.id.toolbar); |
33 | | - webView = findViewById(R.id.web_view); |
34 | | - progress = findViewById(R.id.progress); |
35 | | - setSupportActionBar(toolbar); |
36 | | - getSupportActionBar().setDisplayHomeAsUpEnabled(true); |
37 | | - webView.setProgressBar(progress); |
38 | | - webView.addOnPageStartedListener((v, url, ic)-> { |
39 | | - getSupportActionBar().setSubtitle(url); |
40 | | - }); |
41 | | - |
42 | | - Intent intent = getIntent(); |
43 | | - String indexHtml = intent.getStringExtra("index.html"); |
44 | | - |
45 | | - if (indexHtml != null && !indexHtml.isBlank()) { |
46 | | - webView.loadUrl("file://"+indexHtml); |
47 | | - } else if (Intent.ACTION_VIEW.equals(intent.getAction())) { |
48 | | - Uri data = intent.getData(); |
49 | | - if (data != null) { |
50 | | - loadHtmlFromUri(data); |
51 | | - } else { |
52 | | - webView.loadData("File found'n", "text/html", "UTF-8"); |
| 34 | + try { |
| 35 | + setContentView(R.layout.activity_preview); |
| 36 | + toolbar = findViewById(R.id.toolbar); |
| 37 | + webView = findViewById(R.id.web_view); |
| 38 | + progress = findViewById(R.id.progress); |
| 39 | + swipeRefresh = findViewById(R.id.refresh_layout); |
| 40 | + setSupportActionBar(toolbar); |
| 41 | + getSupportActionBar().setDisplayHomeAsUpEnabled(true); |
| 42 | + webView.setProgressBar(progress); |
| 43 | + webView.addOnPageStartedListener( |
| 44 | + (v, url, ic) -> { |
| 45 | + |
| 46 | + String str = url.substring(url.lastIndexOf("/")+1, url.length()); |
| 47 | + getSupportActionBar().setSubtitle(str); |
| 48 | + }); |
| 49 | + |
| 50 | + Intent intent = getIntent(); |
| 51 | + String indexHtml = intent.getStringExtra("index.html"); |
| 52 | + |
| 53 | + if (indexHtml != null && !indexHtml.isBlank()) { |
| 54 | + webView.loadUrl("file://" + indexHtml); |
| 55 | + } else if (Intent.ACTION_VIEW.equals(intent.getAction())) { |
| 56 | + Uri data = intent.getData(); |
| 57 | + if (data != null) { |
| 58 | + loadHtmlFromUri(data); |
| 59 | + } else { |
| 60 | + webView.loadData("File found'n", "text/html", "UTF-8"); |
| 61 | + } |
53 | 62 | } |
| 63 | + swipeRefresh.setOnRefreshListener( |
| 64 | + () -> { |
| 65 | + webView.reload(); |
| 66 | + swipeRefresh.setRefreshing(false); |
| 67 | + }); |
| 68 | + } catch (Exception err) { |
| 69 | + ErrorDialog.show(this, err); |
54 | 70 | } |
55 | 71 | } |
56 | | - |
| 72 | + |
57 | 73 | @Override |
58 | 74 | public boolean onOptionsItemSelected(MenuItem item) { |
59 | 75 | int id = item.getItemId(); |
60 | | - if(id == android.R.id.home) { |
| 76 | + if (id == android.R.id.home) { |
61 | 77 | finish(); |
62 | 78 | } |
63 | 79 | return super.onOptionsItemSelected(item); |
64 | 80 | } |
65 | | - |
| 81 | + |
66 | 82 | @Override |
67 | 83 | public void handleBackPressed() { |
68 | | - finish(); |
| 84 | + finish(); |
69 | 85 | } |
70 | 86 |
|
71 | 87 | private void loadHtmlFromUri(Uri uri) { |
|
0 commit comments