|
| 1 | +package org.xedox.webaide; |
| 2 | + |
| 3 | +import android.content.Intent; |
| 4 | +import android.graphics.Bitmap; |
| 5 | +import android.net.Uri; |
| 6 | +import android.os.Bundle; |
| 7 | +import android.view.MenuItem; |
| 8 | +import android.view.View; |
| 9 | +import android.webkit.WebChromeClient; |
| 10 | +import android.webkit.WebResourceRequest; |
| 11 | +import android.webkit.WebView; |
| 12 | +import android.webkit.WebViewClient; |
| 13 | +import android.widget.ProgressBar; |
| 14 | +import androidx.appcompat.app.AppCompatActivity; |
| 15 | +import com.google.android.material.appbar.MaterialToolbar; |
| 16 | +import java.io.BufferedReader; |
| 17 | +import java.io.InputStream; |
| 18 | +import java.io.InputStreamReader; |
| 19 | +import org.xedox.utils.BaseActivity; |
| 20 | +import org.xedox.utils.view.WebViewX; |
| 21 | + |
| 22 | +public class PreviewActivity extends BaseActivity { |
| 23 | + |
| 24 | + private MaterialToolbar toolbar; |
| 25 | + private WebViewX webView; |
| 26 | + private ProgressBar progress; |
| 27 | + |
| 28 | + @Override |
| 29 | + protected void onCreate(Bundle savedInstanceState) { |
| 30 | + 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"); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 59 | + int id = item.getItemId(); |
| 60 | + if(id == android.R.id.home) { |
| 61 | + finish(); |
| 62 | + } |
| 63 | + return super.onOptionsItemSelected(item); |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + @Override |
| 68 | + public void handleBackPressed() { |
| 69 | + finish(); |
| 70 | + } |
| 71 | + |
| 72 | + private void loadHtmlFromUri(Uri uri) { |
| 73 | + try (InputStream is = getContentResolver().openInputStream(uri); |
| 74 | + BufferedReader br = new BufferedReader(new InputStreamReader(is))) { |
| 75 | + StringBuilder buffer = new StringBuilder(); |
| 76 | + String line; |
| 77 | + while ((line = br.readLine()) != null) { |
| 78 | + buffer.append(line).append("\n"); |
| 79 | + } |
| 80 | + webView.loadData(buffer.toString(), "text/html", "UTF-8"); |
| 81 | + } catch (Exception e) { |
| 82 | + webView.loadData("Ошибка загрузки: " + e.getMessage(), "text/html", "UTF-8"); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments