Skip to content

Instantly share code, notes, and snippets.

@ahmeturganci
Created October 7, 2019 06:49
Show Gist options
  • Save ahmeturganci/d8d29d42dfc4732849e49dfbf99f92ad to your computer and use it in GitHub Desktop.
Save ahmeturganci/d8d29d42dfc4732849e49dfbf99f92ad to your computer and use it in GitHub Desktop.
package com.example.webview;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.PowerManager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.webkit.ConsoleMessage;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.PermissionRequest;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import java.lang.reflect.Method;
public class MainActivity extends AppCompatActivity {
private final static int INTERVAL = 1000 * 60 * 1; //1 minutes
public static final int RESULT_ENABLE = 11;
private String WEBVIEW_URL;
protected WebView webView;
protected Button button;
private DevicePolicyManager devicePolicyManager;
private ComponentName compName;
protected WebSettings settings;
private PermissionRequest mPermissionRequest;
private PowerManager.WakeLock wl;
private Handler mHandler = new Handler();
public static final String TAG = "LockMonitor-SLM";
private Intent intent;
private int dimTime, sleepTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WEBVIEW_URL = "http://hbizs141";
dimTime = INTERVAL;
sleepTime = INTERVAL * 2;
devicePolicyManager = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
compName = new ComponentName(this, MyAdmin.class);
webView = findViewById(R.id.web1);
boolean mHeldDown = false;
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//Here my web view settings
settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true);
settings.setDatabaseEnabled(true);
settings.setAllowFileAccess(true);
settings.setAppCacheEnabled(true);
settings.setAllowFileAccessFromFileURLs(true);
settings.setAllowUniversalAccessFromFileURLs(true);
settings.setLoadWithOverviewMode(true);
settings.setUseWideViewPort(true);
settings.setAllowContentAccess(true);
webView.setInitialScale(100);
settings.setMediaPlaybackRequiresUserGesture(false);
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient(){
@SuppressWarnings("deprecation")
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
Log.i(TAG, "loading: deprecation");
return true;
//return super.shouldOverrideUrlLoading(view, url);
}
@Override
@TargetApi(Build.VERSION_CODES.N)
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
Log.i(TAG, "loading: build.VERSION_CODES.N");
return true;
//return super.shouldOverrideUrlLoading(view, request);
}
@Override
public void onPageStarted(
WebView view, String url, Bitmap favicon) {
Log.i(TAG, "page started:"+url);
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, final String url) {
Log.i(TAG, "page finished:"+url);
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError er) {
handler.proceed();
}
});
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onPermissionRequest(final PermissionRequest request) {
request.grant(request.getResources());
}
});
//show web application consoles
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onPermissionRequest(final PermissionRequest request) {
mPermissionRequest = request;
final String[] requestedResources = request.getResources();
mPermissionRequest.grant(new String[]{PermissionRequest.RESOURCE_VIDEO_CAPTURE});
}
@Override
public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Log.d("SDMWEB", consoleMessage.message() + " -- From line "
+ consoleMessage.lineNumber() + " of "
+ consoleMessage.sourceId());
return super.onConsoleMessage(consoleMessage);
}
});
webView.getSettings().setUserAgentString("Android WebView");
webView.reload();
final SwipeRefreshLayout mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mSwipeRefreshLayout.setRefreshing(false);
webView.reload();
}
}, 3000);
}
});
webView.loadUrl(WEBVIEW_URL);
/*button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d("RELOAD", "Web View Reload Status");
webView.reload();
}
});*/
getLogsAboutDevice();
}
public static String getSerialNumber() {
String serialNumber;
try {
Class<?> c = Class.forName("android.os.SystemProperties");
Method get = c.getMethod("get", String.class);
serialNumber = (String) get.invoke(c, "gsm.sn1");
if (serialNumber.equals(""))
serialNumber = (String) get.invoke(c, "ril.serialnumber");
if (serialNumber.equals(""))
serialNumber = (String) get.invoke(c, "ro.serialno");
if (serialNumber.equals(""))
serialNumber = (String) get.invoke(c, "sys.serialnumber");
if (serialNumber.equals(""))
serialNumber = Build.SERIAL;
// If none of the methods above worked
if (serialNumber.equals(""))
serialNumber = null;
} catch (Exception e) {
e.printStackTrace();
serialNumber = null;
}
return serialNumber;
}
private void setScreenBrightness(float value) {
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = value;
getWindow().setAttributes(layoutParams);
}
private void setValues() {
Log.d("setValues", "setValues status");
mHandler.removeCallbacks(mUpdateTimeTask);
mHandler.postDelayed(mUpdateTimeTask, dimTime);
mHandler.removeCallbacks(mSleepDevice);
mHandler.postDelayed(mSleepDevice, sleepTime);
}
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
setScreenBrightness(0F);
}
};
private Runnable mSleepDevice = new Runnable() {
public void run() {
intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, compName);
startActivityForResult(intent, RESULT_ENABLE);
boolean isActive = devicePolicyManager.isAdminActive(compName);
devicePolicyManager.lockNow();
}
};
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
hideSystemUI();
}
}
private void hideSystemUI() {
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_IMMERSIVE
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_HOME))
return true;
if((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN))
return true;
if((keyCode == KeyEvent.KEYCODE_VOLUME_UP))
return true;
if ((keyCode == KeyEvent.KEYCODE_BACK))
return true;
if ((keyCode == KeyEvent.KEYCODE_MENU))
return true;
return false;
}
@Override
public void onResume() {
super.onResume();
setValues();
setScreenBrightness(1F);
Log.d("onResume", "onResume status");
}
@SuppressLint("ClickableViewAccessibility")
@Override
public void onStart() {
super.onStart();
webView.setOnTouchListener(new View.OnTouchListener() {
boolean mHeldDown = false;
public boolean onTouch(View arg0, MotionEvent arg1) {
if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
mHeldDown = true;
Thread thread = new Thread() {
public void run() {
try {
while (mHeldDown) {
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
if (mHeldDown) {
Log.d("onTouch", "Touched Brightness Full");
setScreenBrightness(1F);
} else {
Log.d("onTouch_Leave", "Touched LEAVE");
setValues();
}
}
});
sleep(100);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();
}
if (arg1.getAction() == MotionEvent.ACTION_UP) {
mHeldDown = false;
setValues();
Log.d("onTouch_Leave", "Touched LEAVE");
}
return false;
}
});
Log.d("onStart", "onStart status");
}
private void getLogsAboutDevice(){
Log.i("TAG", "SERIAL: " + Build.SERIAL);
Log.i("TAG","MODEL: " + Build.MODEL);
Log.i("TAG","ID: " + Build.ID);
Log.i("TAG","Manufacture: " + Build.MANUFACTURER);
Log.i("TAG","brand: " + Build.BRAND);
Log.i("TAG","type: " + Build.TYPE);
Log.i("TAG","user: " + Build.USER);
Log.i("TAG","BASE: " + Build.VERSION_CODES.BASE);
Log.i("TAG","INCREMENTAL " + Build.VERSION.INCREMENTAL);
Log.i("TAG","SDK " + Build.VERSION.SDK);
Log.i("TAG","BOARD: " + Build.BOARD);
Log.i("TAG","BRAND " + Build.BRAND);
Log.i("TAG","HOST " + Build.HOST);
Log.i("TAG","FINGERPRINT: "+Build.FINGERPRINT);
Log.i("TAG","Version Code: " + Build.VERSION.RELEASE);
Log.i("TAG","Product:" + Build.PRODUCT);
Log.i("TAG","Tags:" + Build.TAGS);
Log.i("TAG","Display:" + Build.DISPLAY);
Log.i("TAG","Serial 2:" + getSerialNumber());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment