| 1 |
|
| 2 |
package io.castleengine; |
| 3 |
|
| 4 |
import android.view.View; |
| 5 |
import android.os.Build; |
| 6 |
import android.content.Intent; |
| 7 |
import android.content.Context; |
| 8 |
import android.content.res.TypedArray; |
| 9 |
import android.net.Uri; |
| 10 |
import android.widget.Toast; |
| 11 |
import android.app.Service; |
| 12 |
import android.view.inputmethod.InputMethodManager; |
| 13 |
import android.view.KeyEvent; |
| 14 |
import android.view.Window; |
| 15 |
|
| 16 |
import androidx.core.view.ViewCompat; |
| 17 |
import androidx.core.view.WindowInsetsControllerCompat; |
| 18 |
import androidx.core.view.WindowInsetsCompat; |
| 19 |
import androidx.core.graphics.Insets; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public class ServiceMiscellaneous extends ServiceAbstract |
| 26 |
{ |
| 27 |
private static final String CATEGORY = "ServiceMiscellaneous"; |
| 28 |
|
| 29 |
public ServiceMiscellaneous(MainActivity activity) |
| 30 |
{ |
| 31 |
super(activity); |
| 32 |
} |
| 33 |
|
| 34 |
public String getName() |
| 35 |
{ |
| 36 |
return "miscellaneous"; |
| 37 |
} |
| 38 |
|
| 39 |
@Override |
| 40 |
public void onCreate() |
| 41 |
{ |
| 42 |
super.onCreate(); |
| 43 |
|
| 44 |
Window window = getActivity().getWindow(); |
| 45 |
View decorView = window.getDecorView(); |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
ViewCompat.setOnApplyWindowInsetsListener(decorView, (v, windowInsets) -> { |
| 54 |
Insets insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars()); |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
messageSend(new String[]{"safe-borders", |
| 60 |
Integer.toString(insets.top), |
| 61 |
Integer.toString(insets.right), |
| 62 |
Integer.toString(insets.bottom), |
| 63 |
Integer.toString(insets.left) |
| 64 |
}); |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
return WindowInsetsCompat.CONSUMED; |
| 71 |
}); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
private void hideSystemBars(View decorView) |
| 76 |
{ |
| 77 |
|
| 78 |
|
| 79 |
WindowInsetsControllerCompat windowInsetsController = |
| 80 |
ViewCompat.getWindowInsetsController(decorView); |
| 81 |
if (windowInsetsController == null) { |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
windowInsetsController.setSystemBarsBehavior( |
| 86 |
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE |
| 87 |
); |
| 88 |
|
| 89 |
windowInsetsController.hide(WindowInsetsCompat.Type.systemBars()); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
@Override |
| 94 |
public void onWindowFocusChanged(boolean hasFocus) |
| 95 |
{ |
| 96 |
if (hasFocus) { |
| 97 |
Window window = getActivity().getWindow(); |
| 98 |
View decorView = window.getDecorView(); |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
int[] attrs = {android.R.attr.windowFullscreen}; |
| 104 |
TypedArray ta = getActivity().getTheme().obtainStyledAttributes(attrs); |
| 105 |
boolean fullscreen = ta.getBoolean(0, false); |
| 106 |
|
| 107 |
if (fullscreen) { |
| 108 |
hideSystemBars(decorView); |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
private void shareText(String title, String subject, String text) |
| 120 |
{ |
| 121 |
Intent sendIntent = new Intent(); |
| 122 |
sendIntent.setAction(Intent.ACTION_SEND); |
| 123 |
sendIntent.putExtra(Intent.EXTRA_TEXT, text); |
| 124 |
sendIntent.putExtra(Intent.EXTRA_SUBJECT, subject); |
| 125 |
sendIntent.setType("text/plain"); |
| 126 |
getActivity().startActivity(Intent.createChooser(sendIntent, title)); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
private void viewUrl(String url) |
| 134 |
{ |
| 135 |
Intent intent = new Intent(Intent.ACTION_VIEW); |
| 136 |
intent.setData(Uri.parse(url)); |
| 137 |
getActivity().startActivity(intent); |
| 138 |
} |
| 139 |
|
| 140 |
@Override |
| 141 |
public boolean messageReceived(String[] parts) |
| 142 |
{ |
| 143 |
if (parts.length == 2 && parts[0].equals("view-url")) { |
| 144 |
viewUrl(parts[1]); |
| 145 |
return true; |
| 146 |
} else |
| 147 |
if (parts.length == 4 && parts[0].equals("share-text")) { |
| 148 |
shareText(parts[1], parts[2], parts[3]); |
| 149 |
return true; |
| 150 |
} else |
| 151 |
if (parts.length == 2 && parts[0].equals("on-screen-notification")) { |
| 152 |
Toast.makeText(getActivity().getApplicationContext(), parts[1], Toast.LENGTH_SHORT).show(); |
| 153 |
return true; |
| 154 |
} else |
| 155 |
{ |
| 156 |
return false; |
| 157 |
} |
| 158 |
} |
| 159 |
} |