| 1 | package tw.nekomimi.nekogram; |
| 2 | |
| 3 | import android.annotation.SuppressLint; |
| 4 | import android.app.Activity; |
| 5 | import android.content.Context; |
| 6 | import android.os.Build; |
| 7 | import android.text.TextUtils; |
| 8 | import android.util.TypedValue; |
| 9 | import android.view.Gravity; |
| 10 | import android.view.View; |
| 11 | import android.view.ViewGroup; |
| 12 | import android.webkit.CookieManager; |
| 13 | import android.webkit.WebSettings; |
| 14 | import android.webkit.WebView; |
| 15 | import android.webkit.WebViewClient; |
| 16 | import android.widget.FrameLayout; |
| 17 | import android.widget.LinearLayout; |
| 18 | import android.widget.TextView; |
| 19 | |
| 20 | import org.telegram.messenger.AndroidUtilities; |
| 21 | import org.telegram.messenger.FileLog; |
| 22 | import org.telegram.messenger.LocaleController; |
| 23 | import org.telegram.messenger.R; |
| 24 | import org.telegram.messenger.browser.Browser; |
| 25 | import org.telegram.ui.ActionBar.BottomSheet; |
| 26 | import org.telegram.ui.ActionBar.Theme; |
| 27 | import org.telegram.ui.Components.LayoutHelper; |
| 28 | import org.telegram.ui.Components.RadialProgressView; |
| 29 | |
| 30 | import java.io.UnsupportedEncodingException; |
| 31 | import java.net.URLEncoder; |
| 32 | |
| 33 | public class TranslateBottomSheet extends BottomSheet { |
| 34 | |
| 35 | @SuppressLint("StaticFieldLeak") |
| 36 | private static TranslateBottomSheet instance; |
| 37 | private WebView webView; |
| 38 | private RadialProgressView progressBar; |
| 39 | private Activity parentActivity; |
| 40 | private FrameLayout containerLayout; |
| 41 | private String url; |
| 42 | |
| 43 | @SuppressLint("SetJavaScriptEnabled") |
| 44 | protected TranslateBottomSheet(Context context, final String text) { |
| 45 | super(context, false); |
| 46 | setApplyTopPadding(false); |
| 47 | setApplyBottomPadding(false); |
| 48 | setCanDismissWithSwipe(false); |
| 49 | |
| 50 | if (context instanceof Activity) { |
| 51 | parentActivity = (Activity) context; |
| 52 | } |
| 53 | |
| 54 | try { |
| 55 | url = String.format("https://translate.google.com/?view=home&op=translate&text=%s", URLEncoder.encode(text, "UTF-8")); |
| 56 | URLEncoder.encode(text, "UTF-8"); |
| 57 | } catch (UnsupportedEncodingException e) { |
| 58 | e.printStackTrace(); |
| 59 | } |
| 60 | |
| 61 | containerLayout = new FrameLayout(context) { |
| 62 | @Override |
| 63 | protected void onDetachedFromWindow() { |
| 64 | super.onDetachedFromWindow(); |
| 65 | try { |
| 66 | if ((webView.getVisibility() != VISIBLE) && webView.getParent() != null) { |
| 67 | removeView(webView); |
| 68 | webView.stopLoading(); |
| 69 | webView.loadUrl("about:blank"); |
| 70 | webView.destroy(); |
| 71 | } |
| 72 | } catch (Exception e) { |
| 73 | FileLog.e(e); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 79 | int h = (int) (AndroidUtilities.displaySize.y / 1.5); |
| 80 | super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(h + AndroidUtilities.dp(48) + 1, MeasureSpec.EXACTLY)); |
| 81 | } |
| 82 | }; |
| 83 | containerLayout.setOnTouchListener((v, event) -> true); |
| 84 | setCustomView(containerLayout); |
| 85 | |
| 86 | webView = new WebView(context); |
| 87 | webView.getSettings().setJavaScriptEnabled(true); |
| 88 | webView.getSettings().setDomStorageEnabled(true); |
| 89 | if (Build.VERSION.SDK_INT >= 17) { |
| 90 | webView.getSettings().setMediaPlaybackRequiresUserGesture(false); |
| 91 | } |
| 92 | |
| 93 | if (Build.VERSION.SDK_INT >= 21) { |
| 94 | webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); |
| 95 | CookieManager cookieManager = CookieManager.getInstance(); |
| 96 | cookieManager.setAcceptThirdPartyCookies(webView, true); |
| 97 | } |
| 98 | |
| 99 | webView.setWebViewClient(new WebViewClient() { |
| 100 | @Override |
| 101 | public void onPageFinished(WebView view, String url) { |
| 102 | super.onPageFinished(view, url); |
| 103 | progressBar.setVisibility(View.INVISIBLE); |
| 104 | |
| 105 | } |
| 106 | }); |
| 107 | |
| 108 | containerLayout.addView(webView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, Gravity.TOP | Gravity.LEFT, 0, 0, 0, 48)); |
| 109 | |
| 110 | progressBar = new RadialProgressView(context); |
| 111 | progressBar.setVisibility(View.INVISIBLE); |
| 112 | containerLayout.addView(progressBar, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.CENTER, 0, 0, 0, (48) / 2)); |
| 113 | |
| 114 | View lineView = new View(context); |
| 115 | lineView.setBackgroundColor(Theme.getColor(Theme.key_dialogGrayLine)); |
| 116 | containerLayout.addView(lineView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1, Gravity.LEFT | Gravity.BOTTOM)); |
| 117 | ((FrameLayout.LayoutParams) lineView.getLayoutParams()).bottomMargin = AndroidUtilities.dp(48); |
| 118 | |
| 119 | FrameLayout frameLayout = new FrameLayout(context); |
| 120 | frameLayout.setBackgroundColor(Theme.getColor(Theme.key_dialogBackground)); |
| 121 | containerLayout.addView(frameLayout, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 48, Gravity.LEFT | Gravity.BOTTOM)); |
| 122 | |
| 123 | LinearLayout linearLayout = new LinearLayout(context); |
| 124 | linearLayout.setOrientation(LinearLayout.HORIZONTAL); |
| 125 | linearLayout.setWeightSum(1); |
| 126 | frameLayout.addView(linearLayout, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.MATCH_PARENT, Gravity.TOP | Gravity.RIGHT)); |
| 127 | |
| 128 | TextView textView = new TextView(context); |
| 129 | textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14); |
| 130 | textView.setTextColor(Theme.getColor(Theme.key_dialogTextBlue4)); |
| 131 | textView.setGravity(Gravity.CENTER); |
| 132 | textView.setSingleLine(true); |
| 133 | textView.setEllipsize(TextUtils.TruncateAt.END); |
| 134 | textView.setBackgroundDrawable(Theme.createSelectorDrawable(Theme.getColor(Theme.key_dialogButtonSelector), 0)); |
| 135 | textView.setPadding(AndroidUtilities.dp(18), 0, AndroidUtilities.dp(18), 0); |
| 136 | textView.setText(LocaleController.getString("Close", R.string.Close).toUpperCase()); |
| 137 | textView.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf")); |
| 138 | frameLayout.addView(textView, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.MATCH_PARENT, Gravity.TOP | Gravity.LEFT)); |
| 139 | textView.setOnClickListener(v -> dismiss()); |
| 140 | |
| 141 | TextView openInButton = new TextView(context); |
| 142 | openInButton.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14); |
| 143 | openInButton.setTextColor(Theme.getColor(Theme.key_dialogTextBlue4)); |
| 144 | openInButton.setGravity(Gravity.CENTER); |
| 145 | openInButton.setSingleLine(true); |
| 146 | openInButton.setEllipsize(TextUtils.TruncateAt.END); |
| 147 | openInButton.setBackgroundDrawable(Theme.createSelectorDrawable(Theme.getColor(Theme.key_dialogButtonSelector), 0)); |
| 148 | openInButton.setPadding(AndroidUtilities.dp(18), 0, AndroidUtilities.dp(18), 0); |
| 149 | openInButton.setText(LocaleController.getString("OpenInBrowser", R.string.OpenInBrowser).toUpperCase()); |
| 150 | openInButton.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf")); |
| 151 | linearLayout.addView(openInButton, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.MATCH_PARENT, Gravity.TOP | Gravity.LEFT)); |
| 152 | openInButton.setOnClickListener(v -> { |
| 153 | Browser.openUrl(parentActivity, url); |
| 154 | dismiss(); |
| 155 | }); |
| 156 | |
| 157 | setDelegate(new BottomSheet.BottomSheetDelegate() { |
| 158 | @Override |
| 159 | public void onOpenAnimationEnd() { |
| 160 | progressBar.setVisibility(View.VISIBLE); |
| 161 | webView.setVisibility(View.VISIBLE); |
| 162 | try { |
| 163 | webView.loadUrl(url); |
| 164 | } catch (Exception e) { |
| 165 | FileLog.e(e); |
| 166 | } |
| 167 | |
| 168 | } |
| 169 | |
| 170 | }); |
| 171 | |
| 172 | instance = this; |
| 173 | } |
| 174 | |
| 175 | public static void show(Context context, final String text) { |
| 176 | if (instance != null) { |
| 177 | instance.destroy(); |
| 178 | } |
| 179 | new TranslateBottomSheet(context, text).show(); |
| 180 | } |
| 181 | |
| 182 | public static TranslateBottomSheet getInstance() { |
| 183 | return instance; |
| 184 | } |
| 185 | |
| 186 | public void destroy() { |
| 187 | if (webView != null && webView.getVisibility() == View.VISIBLE) { |
| 188 | containerLayout.removeView(webView); |
| 189 | webView.stopLoading(); |
| 190 | webView.loadUrl("about:blank"); |
| 191 | webView.destroy(); |
| 192 | } |
| 193 | instance = null; |
| 194 | dismissInternal(); |
| 195 | } |
| 196 | |
| 197 | } |
| 198 | |