|
1 | 1 | package org.xedox.webaide; |
2 | 2 |
|
| 3 | +import android.content.ActivityNotFoundException; |
3 | 4 | import android.content.Intent; |
4 | 5 | import android.graphics.Bitmap; |
5 | 6 | import android.net.Uri; |
6 | 7 | import android.os.Bundle; |
| 8 | +import android.view.Menu; |
7 | 9 | import android.view.MenuItem; |
8 | | -import android.view.View; |
9 | 10 | import android.webkit.WebChromeClient; |
10 | | -import android.webkit.WebResourceRequest; |
| 11 | +import android.webkit.WebSettings; |
11 | 12 | import android.webkit.WebView; |
12 | 13 | import android.webkit.WebViewClient; |
13 | 14 | import android.widget.ProgressBar; |
14 | 15 | import androidx.appcompat.app.AppCompatActivity; |
15 | 16 | import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; |
16 | 17 | import com.google.android.material.appbar.MaterialToolbar; |
17 | 18 | import java.io.BufferedReader; |
| 19 | +import java.io.File; |
| 20 | +import java.io.FileInputStream; |
18 | 21 | import java.io.InputStream; |
19 | 22 | import java.io.InputStreamReader; |
20 | | -import org.xedox.utils.BaseActivity; |
| 23 | +import java.io.IOException; |
| 24 | +import fi.iki.elonen.NanoHTTPD; |
21 | 25 | import org.xedox.utils.dialog.ErrorDialog; |
22 | 26 | import org.xedox.utils.view.WebViewX; |
23 | 27 |
|
24 | | -public class PreviewActivity extends BaseActivity { |
| 28 | +public class PreviewActivity extends AppCompatActivity { |
25 | 29 |
|
26 | 30 | private MaterialToolbar toolbar; |
27 | 31 | private WebViewX webView; |
28 | 32 | private ProgressBar progress; |
29 | 33 | private SwipeRefreshLayout swipeRefresh; |
30 | 34 |
|
| 35 | + private boolean isDesktopMode = false; |
| 36 | + private String currentUrl = null; |
| 37 | + private LocalWebServer localServer = null; |
| 38 | + private String localServerUrl = null; |
| 39 | + |
31 | 40 | @Override |
32 | 41 | protected void onCreate(Bundle savedInstanceState) { |
33 | 42 | super.onCreate(savedInstanceState); |
34 | 43 | try { |
35 | 44 | setContentView(R.layout.activity_preview); |
| 45 | + |
36 | 46 | toolbar = findViewById(R.id.toolbar); |
37 | 47 | webView = findViewById(R.id.web_view); |
38 | 48 | progress = findViewById(R.id.progress); |
39 | 49 | swipeRefresh = findViewById(R.id.refresh_layout); |
| 50 | + |
| 51 | + setSupportActionBar(toolbar); |
| 52 | + getSupportActionBar().setDisplayHomeAsUpEnabled(true); |
| 53 | + webView.setProgressBar(progress); |
| 54 | + |
| 55 | + WebSettings settings = webView.getSettings(); |
| 56 | + settings.setJavaScriptEnabled(true); |
| 57 | + settings.setDomStorageEnabled(true); |
| 58 | + settings.setAllowFileAccess(true); |
| 59 | + settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); |
| 60 | + |
| 61 | + webView.setWebViewClient(new WebViewClient() { |
| 62 | + @Override |
| 63 | + public void onPageStarted(WebView view, String url, Bitmap favicon) { |
| 64 | + currentUrl = url; |
| 65 | + progress.setVisibility(ProgressBar.VISIBLE); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void onPageFinished(WebView view, String url) { |
| 70 | + currentUrl = url; |
| 71 | + progress.setVisibility(ProgressBar.GONE); |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + webView.setWebChromeClient(new WebChromeClient() { |
| 76 | + @Override |
| 77 | + public void onProgressChanged(WebView view, int newProgress) { |
| 78 | + progress.setProgress(newProgress); |
| 79 | + if (newProgress >= 100) { |
| 80 | + progress.setVisibility(ProgressBar.GONE); |
| 81 | + } else { |
| 82 | + progress.setVisibility(ProgressBar.VISIBLE); |
| 83 | + } |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + Intent intent = getIntent(); |
| 88 | + String indexHtml = intent.getStringExtra("index.html"); |
| 89 | + |
| 90 | + if (indexHtml != null && !indexHtml.isBlank()) { |
| 91 | + File htmlFile = new File(indexHtml); |
| 92 | + if (htmlFile.exists()) { |
| 93 | + startLocalServer(htmlFile.getParentFile()); |
| 94 | + localServerUrl = "http://localhost:8080/" + htmlFile.getName(); |
| 95 | + webView.loadUrl(localServerUrl); |
| 96 | + currentUrl = localServerUrl; |
| 97 | + } else { |
| 98 | + webView.loadData("File not found", "text/html", "UTF-8"); |
| 99 | + } |
| 100 | + } else if (Intent.ACTION_VIEW.equals(intent.getAction())) { |
| 101 | + Uri data = intent.getData(); |
| 102 | + if (data != null) { |
| 103 | + loadHtmlFromUri(data); |
| 104 | + currentUrl = data.toString(); |
| 105 | + } else { |
| 106 | + webView.loadData("File not found", "text/html", "UTF-8"); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + swipeRefresh.setOnRefreshListener(() -> { |
| 111 | + webView.reload(); |
| 112 | + swipeRefresh.setRefreshing(false); |
| 113 | + }); |
| 114 | + |
| 115 | + setWebViewMode(false); |
| 116 | + |
| 117 | + } catch (Exception err) { |
| 118 | + ErrorDialog.show(this, err); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public boolean onCreateOptionsMenu(Menu menu) { |
| 124 | + getMenuInflater().inflate(R.menu.menu_preview, menu); |
| 125 | + return true; |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 130 | + int id = item.getItemId(); |
| 131 | + |
| 132 | + if (id == android.R.id.home) { |
| 133 | + finish(); |
| 134 | + } else if (id == R.id.action_open_browser) { |
| 135 | + if (currentUrl != null) { |
| 136 | + try { |
| 137 | + Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(currentUrl)); |
| 138 | + browserIntent.addCategory(Intent.CATEGORY_BROWSABLE); |
| 139 | + browserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 140 | + startActivity(browserIntent); |
| 141 | + } catch (ActivityNotFoundException e) { |
| 142 | + ErrorDialog.show(this, new Exception("Tidak dapat membuka browser.")); |
| 143 | + } |
| 144 | + } else { |
| 145 | + ErrorDialog.show(this, new Exception("Tidak ada URL yang bisa dibuka di browser.")); |
| 146 | + } |
| 147 | + return true; |
| 148 | + |
| 149 | + } else if (id == R.id.action_toggle_mode) { |
| 150 | + isDesktopMode = !isDesktopMode; |
| 151 | + setWebViewMode(isDesktopMode); |
| 152 | + item.setTitle(isDesktopMode ? "Switch to Phone" : "Switch to Desktop"); |
| 153 | + webView.reload(); |
| 154 | + return true; |
| 155 | + } |
| 156 | + |
| 157 | + return super.onOptionsItemSelected(item); |
| 158 | + } |
| 159 | + |
| 160 | + private void setWebViewMode(boolean desktopMode) { |
| 161 | + WebSettings settings = webView.getSettings(); |
| 162 | + if (desktopMode) { |
| 163 | + settings.setUserAgentString( |
| 164 | + "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 " + |
| 165 | + "(KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"); |
| 166 | + settings.setUseWideViewPort(true); |
| 167 | + settings.setLoadWithOverviewMode(true); |
| 168 | + webView.setInitialScale(100); |
| 169 | + } else { |
| 170 | + settings.setUserAgentString( |
| 171 | + "Mozilla/5.0 (Linux; Android 10) AppleWebKit/537.36 " + |
| 172 | + "(KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36"); |
| 173 | + settings.setUseWideViewPort(false); |
| 174 | + settings.setLoadWithOverviewMode(false); |
| 175 | + webView.setInitialScale(0); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + private void loadHtmlFromUri(Uri uri) { |
| 180 | + try (InputStream is = getContentResolver().openInputStream(uri); |
| 181 | + BufferedReader br = new BufferedReader(new InputStreamReader(is))) { |
| 182 | + StringBuilder buffer = new StringBuilder(); |
| 183 | + String line; |
| 184 | + while ((line = br.readLine()) != null) { |
| 185 | + buffer.append(line).append("\n"); |
| 186 | + } |
| 187 | + webView.loadData(buffer.toString(), "text/html", "UTF-8"); |
| 188 | + } catch (Exception e) { |
| 189 | + webView.loadData("Error loading file: " + e.getMessage(), "text/html", "UTF-8"); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + @Override |
| 194 | + protected void onDestroy() { |
| 195 | + super.onDestroy(); |
| 196 | + stopLocalServer(); |
| 197 | + } |
| 198 | + |
| 199 | + private void startLocalServer(File rootDir) { |
| 200 | + try { |
| 201 | + stopLocalServer(); |
| 202 | + localServer = new LocalWebServer(8080, rootDir); |
| 203 | + localServer.start(); |
| 204 | + } catch (IOException e) { |
| 205 | + ErrorDialog.show(this, new Exception("Gagal memulai server lokal: " + e.getMessage())); |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + private void stopLocalServer() { |
| 210 | + if (localServer != null) { |
| 211 | + localServer.stop(); |
| 212 | + localServer = null; |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + public static class LocalWebServer extends NanoHTTPD { |
| 217 | + private final File rootDir; |
| 218 | + |
| 219 | + public LocalWebServer(int port, File rootDir) { |
| 220 | + super(port); |
| 221 | + this.rootDir = rootDir; |
| 222 | + } |
| 223 | + |
| 224 | + @Override |
| 225 | + public Response serve(IHTTPSession session) { |
| 226 | + try { |
| 227 | + String uri = session.getUri(); |
| 228 | + File file = new File(rootDir, uri); |
| 229 | + if (!file.exists()) { |
| 230 | + return newFixedLengthResponse(Response.Status.NOT_FOUND, "text/plain", "404 Not Found"); |
| 231 | + } |
| 232 | + FileInputStream fis = new FileInputStream(file); |
| 233 | + String mime = getMimeTypeForFile(uri); |
| 234 | + return newChunkedResponse(Response.Status.OK, mime, fis); |
| 235 | + } catch (Exception e) { |
| 236 | + return newFixedLengthResponse(Response.Status.INTERNAL_ERROR, "text/plain", "Error: " + e.getMessage()); |
| 237 | + } |
| 238 | + } |
| 239 | + } |
| 240 | + } webView = findViewById(R.id.web_view); |
| 241 | + progress = findViewById(R.id.progress); |
| 242 | + swipeRefresh = findViewById(R.id.refresh_layout); |
40 | 243 | setSupportActionBar(toolbar); |
41 | 244 | getSupportActionBar().setDisplayHomeAsUpEnabled(true); |
42 | 245 | webView.setProgressBar(progress); |
|
0 commit comments