Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.83 KB | None | 0 0
  1. package mgks.os.webview;
  2.  
  3. import android.Manifest;
  4. import android.annotation.SuppressLint;
  5. import android.annotation.TargetApi;
  6. import android.app.Activity;
  7. import android.app.ActivityManager;
  8. import android.app.Notification;
  9. import android.app.NotificationManager;
  10. import android.app.PendingIntent;
  11. import android.content.ActivityNotFoundException;
  12. import android.content.Context;
  13. import android.content.Intent;
  14. import android.content.pm.PackageManager;
  15. import android.content.res.Configuration;
  16. import android.graphics.Bitmap;
  17. import android.graphics.BitmapFactory;
  18. import android.media.RingtoneManager;
  19. import android.net.Uri;
  20. import android.os.Build;
  21. import android.os.Environment;
  22. import android.os.Handler;
  23. import android.provider.MediaStore;
  24. import android.provider.Settings;
  25. import android.support.annotation.NonNull;
  26. import android.support.v4.app.ActivityCompat;
  27. import android.support.v4.content.ContextCompat;
  28. import android.support.v7.app.AppCompatActivity;
  29. import android.os.Bundle;
  30. import android.support.v7.app.NotificationCompat;
  31. import android.util.Log;
  32. import android.view.KeyEvent;
  33. import android.view.View;
  34. import android.view.WindowManager;
  35. import android.webkit.CookieManager;
  36. import android.webkit.ValueCallback;
  37. import android.webkit.WebChromeClient;
  38. import android.webkit.WebResourceError;
  39. import android.webkit.WebResourceRequest;
  40. import android.webkit.WebSettings;
  41. import android.webkit.WebView;
  42. import android.webkit.WebViewClient;
  43. import android.widget.ProgressBar;
  44. import android.widget.TextView;
  45. import android.widget.Toast;
  46.  
  47. import java.io.File;
  48. import java.io.IOException;
  49. import java.math.BigInteger;
  50. import java.security.SecureRandom;
  51. import java.text.SimpleDateFormat;
  52. import java.util.Date;
  53.  
  54. public class MainActivity extends AppCompatActivity {
  55.  
  56. static boolean ASWP_JSCRIPT = SmartWebView.ASWP_JSCRIPT;
  57. static boolean ASWP_FUPLOAD = SmartWebView.ASWP_FUPLOAD;
  58. static boolean ASWP_CAMUPLOAD = SmartWebView.ASWP_CAMUPLOAD;
  59. static boolean ASWP_MULFILE = SmartWebView.ASWP_MULFILE;
  60. static boolean ASWP_LOCATION = SmartWebView.ASWP_LOCATION;
  61. static boolean ASWP_RATINGS = SmartWebView.ASWP_RATINGS;
  62. static boolean ASWP_PBAR = SmartWebView.ASWP_PBAR;
  63. static boolean ASWP_ZOOM = SmartWebView.ASWP_ZOOM;
  64. static boolean ASWP_SFORM = SmartWebView.ASWP_SFORM;
  65. static boolean ASWP_OFFLINE = SmartWebView.ASWP_OFFLINE;
  66. static boolean ASWP_EXTURL = SmartWebView.ASWP_EXTURL;
  67.  
  68. //Configuration variables
  69. private static String ASWV_URL = SmartWebView.ASWV_URL;
  70. private static String ASWV_F_TYPE = SmartWebView.ASWV_F_TYPE;
  71.  
  72. public static String ASWV_HOST = aswm_host(ASWV_URL);
  73.  
  74. //Careful with these variable names if altering
  75. WebView asw_view;
  76. ProgressBar asw_progress;
  77. //TextView asw_loading_text;
  78. NotificationManager asw_notification;
  79. Notification asw_notification_new;
  80.  
  81. private String asw_cam_message;
  82. private ValueCallback<Uri> asw_file_message;
  83. private ValueCallback<Uri[]> asw_file_path;
  84. private final static int asw_file_req = 1;
  85.  
  86. private final static int loc_perm = 1;
  87. private final static int file_perm = 2;
  88.  
  89. private SecureRandom random = new SecureRandom();
  90.  
  91. private static final String TAG = MainActivity.class.getSimpleName();
  92.  
  93. @Override
  94. protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
  95. super.onActivityResult(requestCode, resultCode, intent);
  96. if (Build.VERSION.SDK_INT >= 21) {
  97. getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
  98. getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimary));
  99. Uri[] results = null;
  100. if (resultCode == Activity.RESULT_OK) {
  101. if (requestCode == asw_file_req) {
  102. if (null == asw_file_path) {
  103. return;
  104. }
  105. if (intent == null) {
  106. if (asw_cam_message != null) {
  107. results = new Uri[]{Uri.parse(asw_cam_message)};
  108. }
  109. } else {
  110. String dataString = intent.getDataString();
  111. if (dataString != null) {
  112. results = new Uri[]{ Uri.parse(dataString) };
  113. }
  114. }
  115. }
  116. }
  117. asw_file_path.onReceiveValue(results);
  118. asw_file_path = null;
  119. } else {
  120. if (requestCode == asw_file_req) {
  121. if (null == asw_file_message) return;
  122. Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
  123. asw_file_message.onReceiveValue(result);
  124. asw_file_message = null;
  125. }
  126. }
  127. }
  128.  
  129. @SuppressLint({"SetJavaScriptEnabled", "WrongViewCast"})
  130. @Override
  131. protected void onCreate(Bundle savedInstanceState) {
  132. super.onCreate(savedInstanceState);
  133. setContentView(R.layout.activity_main);
  134.  
  135. if (ASWP_PBAR) {
  136. asw_progress = (ProgressBar) findViewById(R.id.msw_progress);
  137. } else {
  138. findViewById(R.id.msw_progress).setVisibility(View.GONE);
  139. }
  140.  
  141. //asw_loading_text = (TextView) findViewById(R.id.msw_loading_text);
  142. Handler handler = new Handler();
  143.  
  144. //Launching app rating request
  145. if (ASWP_RATINGS) {
  146. handler.postDelayed(new Runnable() { public void run() { get_rating(); }}, 1000 * 60); //running request after few moments
  147. }
  148.  
  149. //Getting basic device information
  150. get_info();
  151.  
  152. //Getting GPS location of device if given permission
  153. get_location();
  154.  
  155. asw_view = (WebView) findViewById(R.id.msw_view);
  156.  
  157. //Webview settings; defaults are customized for best performance
  158. WebSettings webSettings = asw_view.getSettings();
  159.  
  160. if(!ASWP_OFFLINE){
  161. webSettings.setJavaScriptEnabled(ASWP_JSCRIPT);
  162. }
  163. webSettings.setSaveFormData(ASWP_SFORM);
  164. webSettings.setSupportZoom(ASWP_ZOOM);
  165. webSettings.setGeolocationEnabled(ASWP_LOCATION);
  166. webSettings.setAllowFileAccess(true);
  167. webSettings.setAllowFileAccessFromFileURLs(true);
  168. webSettings.setAllowUniversalAccessFromFileURLs(true);
  169. webSettings.setUseWideViewPort(true);
  170. webSettings.setDomStorageEnabled(true);
  171. webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
  172.  
  173. if (Build.VERSION.SDK_INT >= 21) {
  174. getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
  175. getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));
  176. asw_view.setLayerType(View.LAYER_TYPE_HARDWARE, null);
  177. webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
  178. } else if (Build.VERSION.SDK_INT >= 19) {
  179. asw_view.setLayerType(View.LAYER_TYPE_HARDWARE, null);
  180. }
  181. asw_view.setVerticalScrollBarEnabled(false);
  182. asw_view.setWebViewClient(new Callback());
  183.  
  184. //Rendering the default URL
  185. aswm_view(ASWV_URL, false);
  186.  
  187. asw_view.setWebChromeClient(new WebChromeClient() {
  188. //Handling input[type="file"] requests for android API 16+
  189. public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
  190. if(ASWP_FUPLOAD) {
  191. asw_file_message = uploadMsg;
  192. Intent i = new Intent(Intent.ACTION_GET_CONTENT);
  193. i.addCategory(Intent.CATEGORY_OPENABLE);
  194. i.setType(ASWV_F_TYPE);
  195. startActivityForResult(Intent.createChooser(i, "File Chooser"), asw_file_req);
  196. }
  197. }
  198. //Handling input[type="file"] requests for android API 21+
  199. public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback,WebChromeClient.FileChooserParams fileChooserParams){
  200. get_file();
  201. if(ASWP_FUPLOAD) {
  202. if (asw_file_path != null) {
  203. asw_file_path.onReceiveValue(null);
  204. }
  205. asw_file_path = filePathCallback;
  206. Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
  207. contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
  208. contentSelectionIntent.setType(ASWV_F_TYPE);
  209. Intent[] intentArray;
  210. if (ASWP_CAMUPLOAD) {
  211. Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  212. if (takePictureIntent.resolveActivity(MainActivity.this.getPackageManager()) != null) {
  213. File photoFile = null;
  214. try {
  215. photoFile = create_image();
  216. takePictureIntent.putExtra("PhotoPath", asw_cam_message);
  217. } catch (IOException ex) {
  218. Log.e(TAG, "Image file creation failed", ex);
  219. }
  220. if (photoFile != null) {
  221. asw_cam_message = "file:" + photoFile.getAbsolutePath();
  222. takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
  223. } else {
  224. takePictureIntent = null;
  225. }
  226. }
  227. if (takePictureIntent != null) {
  228. intentArray = new Intent[]{takePictureIntent};
  229. } else {
  230. intentArray = new Intent[0];
  231. }
  232. } else {
  233. intentArray = new Intent[0];
  234. }
  235. Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
  236. chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
  237. chooserIntent.putExtra(Intent.EXTRA_TITLE, "File Chooser");
  238. chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
  239. startActivityForResult(chooserIntent, asw_file_req);
  240. }
  241. return true;
  242. }
  243.  
  244. //Getting webview rendering progress
  245. @Override
  246. public void onProgressChanged(WebView view, int p) {
  247. if (ASWP_PBAR) {
  248.  
  249. if(p < 100 && asw_progress.getVisibility() == ProgressBar.GONE){
  250. asw_progress.setVisibility(ProgressBar.VISIBLE);
  251. }
  252.  
  253. asw_progress.setProgress(p);
  254.  
  255. if (p == 100) {
  256. asw_progress.setProgress(0);
  257. }
  258.  
  259. }
  260. }
  261. });
  262. if (getIntent().getData() != null) {
  263. String path = getIntent().getDataString();
  264.  
  265. aswm_view(path, false);
  266. }
  267. }
  268.  
  269. @Override
  270. public void onResume() {
  271. super.onResume();
  272. //Coloring the "recent apps" tab header; doing it onResume, as an insurance
  273. if (Build.VERSION.SDK_INT >= 23) {
  274. Bitmap bm = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
  275. ActivityManager.TaskDescription taskDesc = null;
  276. taskDesc = new ActivityManager.TaskDescription(getString(R.string.app_name), bm, getColor(R.color.colorPrimary));
  277. MainActivity.this.setTaskDescription(taskDesc);
  278. }
  279. get_location();
  280. }
  281.  
  282. //Setting activity layout visibility
  283. private class Callback extends WebViewClient {
  284. public void onPageStarted(WebView view, String url, Bitmap favicon) {
  285. get_location();
  286. }
  287.  
  288. public void onPageFinished(WebView view, String url) {
  289. findViewById(R.id.msw_welcome).setVisibility(View.GONE);
  290. findViewById(R.id.msw_view).setVisibility(View.VISIBLE);
  291. }
  292. //For android below API 23
  293. @SuppressWarnings("deprecation")
  294. @Override
  295. public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
  296. Toast.makeText(getApplicationContext(), "Something Went Wrong!", Toast.LENGTH_SHORT).show();
  297. aswm_view("file:///android_res/raw/error.html", false);
  298. }
  299.  
  300. @Override
  301. public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
  302. Toast.makeText(getApplicationContext(), "Something Went Wrong!", Toast.LENGTH_SHORT).show();
  303. aswm_view("file:///android_res/raw/error.html", false);
  304. }
  305.  
  306. //Overriding webview URLs
  307. @SuppressWarnings("deprecation")
  308. @Override
  309. public boolean shouldOverrideUrlLoading(WebView view, String url) {
  310. return url_actions(view, url);
  311. }
  312.  
  313. //Overriding webview URLs for API 23+ [suggested by github.com/JakePou]
  314. @TargetApi(Build.VERSION_CODES.N)
  315. @Override
  316. public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
  317. return url_actions(view, request.getUrl().toString());
  318. }
  319. }
  320.  
  321. //Random ID creation function to help get fresh cache every-time webview reloaded
  322. public String random_id() {
  323. return new BigInteger(130, random).toString(32);
  324. }
  325.  
  326. //Opening URLs inside webview with request
  327. void aswm_view(String url, Boolean tab) {
  328. if (tab) {
  329. Intent intent = new Intent(Intent.ACTION_VIEW);
  330. intent.setData(Uri.parse(url));
  331. startActivity(intent);
  332. } else {
  333. //asw_view.loadUrl(url+"?rid="+random_id());
  334. asw_view.loadUrl(url);
  335. }
  336. }
  337.  
  338. //Actions based on shouldOverrideUrlLoading
  339. public boolean url_actions(WebView view, String url){
  340. boolean a = true;
  341. //Show toast error if not connected to the network
  342. if (!ASWP_OFFLINE && !DetectConnection.isInternetAvailable(MainActivity.this)) {
  343. Toast.makeText(getApplicationContext(), "Please check your Network Connection!", Toast.LENGTH_SHORT).show();
  344.  
  345. //Use this in a hyperlink to redirect back to default URL :: href="refresh:android"
  346. } else if (url.startsWith("refresh:")) {
  347. aswm_view(ASWV_URL, false);
  348.  
  349. //Use this in a hyperlink to launch default phone dialer for specific number :: href="tel:+919876543210"
  350. } else if (url.startsWith("tel:")) {
  351. Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
  352. startActivity(intent);
  353.  
  354. //Use this to open your apps page on google play store app :: href="rate:android"
  355. } else if (url.startsWith("rate:")) {
  356. final String app_package = getPackageName(); //requesting app package name from Context or Activity object
  357. try {
  358. startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + app_package)));
  359. } catch (ActivityNotFoundException anfe) {
  360. startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + app_package)));
  361. }
  362.  
  363. //Sharing content from your webview to external apps :: href="share:URL" and remember to place the URL you want to share after share:___
  364. } else if (url.startsWith("share:")) {
  365. Intent intent = new Intent(Intent.ACTION_SEND);
  366. intent.setType("text/plain");
  367. intent.putExtra(Intent.EXTRA_SUBJECT, view.getTitle());
  368. intent.putExtra(Intent.EXTRA_TEXT, view.getTitle()+"nVisit: "+(Uri.parse(url).toString()).replace("share:",""));
  369. startActivity(Intent.createChooser(intent, "Share with your Friends"));
  370.  
  371. //Use this in a hyperlink to exit your app :: href="exit:android"
  372. } else if (url.startsWith("exit:")) {
  373. Intent intent = new Intent(Intent.ACTION_MAIN);
  374. intent.addCategory(Intent.CATEGORY_HOME);
  375. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  376. startActivity(intent);
  377.  
  378. //Opening external URLs in android default web browser
  379. } else if (ASWP_EXTURL && !aswm_host(url).equals(ASWV_HOST)) {
  380. aswm_view(url,true);
  381. } else {
  382. a = false;
  383. }
  384. return a;
  385. }
  386.  
  387. //Getting host name
  388. public static String aswm_host(String url){
  389. if (url == null || url.length() == 0) {
  390. return "";
  391. }
  392. int dslash = url.indexOf("//");
  393. if (dslash == -1) {
  394. dslash = 0;
  395. } else {
  396. dslash += 2;
  397. }
  398. int end = url.indexOf('/', dslash);
  399. end = end >= 0 ? end : url.length();
  400. int port = url.indexOf(':', dslash);
  401. end = (port > 0 && port < end) ? port : end;
  402. Log.w("URL Host: ",url.substring(dslash, end));
  403. return url.substring(dslash, end);
  404. }
  405.  
  406. //Getting device basic information
  407. public void get_info(){
  408. CookieManager cookieManager = CookieManager.getInstance();
  409. cookieManager.setAcceptCookie(true);
  410. cookieManager.setCookie(ASWV_URL, "DEVICE=android");
  411. cookieManager.setCookie(ASWV_URL, "DEV_API=" + Build.VERSION.SDK_INT);
  412. }
  413.  
  414. //Checking permission for storage and camera for writing and uploading images
  415. public void get_file(){
  416. String[] perms = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA};
  417.  
  418. //Checking for storage permission to write images for upload
  419. if (ASWP_FUPLOAD && ASWP_CAMUPLOAD && !check_permission(2) && !check_permission(3)) {
  420. ActivityCompat.requestPermissions(MainActivity.this, perms, file_perm);
  421.  
  422. //Checking for WRITE_EXTERNAL_STORAGE permission
  423. } else if (ASWP_FUPLOAD && !check_permission(2)) {
  424. ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, file_perm);
  425.  
  426. //Checking for CAMERA permissions
  427. } else if (ASWP_CAMUPLOAD && !check_permission(3)) {
  428. ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, file_perm);
  429. }
  430. }
  431.  
  432. //Using cookies to update user locations
  433. public void get_location(){
  434. CookieManager cookieManager = CookieManager.getInstance();
  435. cookieManager.setAcceptCookie(true);
  436. if(ASWP_LOCATION) {
  437. //Checking for location permissions
  438. if (Build.VERSION.SDK_INT >= 23 && !check_permission(1)) {
  439. ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, loc_perm);
  440. show_notification(2, 2);
  441.  
  442. } else {
  443. GPSTrack gps;
  444. gps = new GPSTrack(MainActivity.this);
  445. double latitude = gps.getLatitude();
  446. double longitude = gps.getLongitude();
  447. if (gps.canGetLocation()) {
  448. if (latitude != 0 || longitude != 0) {
  449. cookieManager.setCookie(ASWV_URL, "lat=" + latitude);
  450. cookieManager.setCookie(ASWV_URL, "long=" + longitude);
  451. //Log.w("New Updated Location:", latitude + "," + longitude); //enable to test dummy latitude and longitude
  452. } else {
  453. Log.w("New Updated Location:", "NULL");
  454. }
  455. } else {
  456. show_notification(1, 1);
  457. Log.w("New Updated Location:", "FAIL");
  458. }
  459. }
  460. }
  461. }
  462.  
  463. //Checking if particular permission is given or not
  464. public boolean check_permission(int permission){
  465. switch(permission){
  466. case 1:
  467. return ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
  468.  
  469. case 2:
  470. return ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
  471.  
  472. case 3:
  473. return ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED;
  474.  
  475. }
  476. return false;
  477. }
  478.  
  479. //Creating image file for upload
  480. private File create_image() throws IOException {
  481. @SuppressLint("SimpleDateFormat")
  482. String file_name = new SimpleDateFormat("yyyy_mm_ss").format(new Date());
  483. String new_name = "file_"+file_name+"_";
  484. File sd_directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
  485. return File.createTempFile(new_name, ".jpg", sd_directory);
  486. }
  487.  
  488. //Launching app rating dialoge [developed by github.com/hotchemi]
  489. public void get_rating() {
  490. if (DetectConnection.isInternetAvailable(MainActivity.this)) {
  491. AppRate.with(this)
  492. .setStoreType(StoreType.GOOGLEPLAY) //default is Google Play, other option is Amazon App Store
  493. .setInstallDays(SmartWebView.ASWR_DAYS)
  494. .setLaunchTimes(SmartWebView.ASWR_TIMES)
  495. .setRemindInterval(SmartWebView.ASWR_INTERVAL)
  496. .setTitle(R.string.rate_dialog_title)
  497. .setMessage(R.string.rate_dialog_message)
  498. .setTextLater(R.string.rate_dialog_cancel)
  499. .setTextNever(R.string.rate_dialog_no)
  500. .setTextRateNow(R.string.rate_dialog_ok)
  501. .monitor();
  502. AppRate.showRateDialogIfMeetsConditions(this);
  503. }
  504. //for more customizations, look for AppRate and DialogManager
  505. }
  506.  
  507. //Creating custom notifications with IDs
  508. public void show_notification(int type, int id) {
  509. long when = System.currentTimeMillis();
  510. asw_notification = (NotificationManager) MainActivity.this.getSystemService(Context.NOTIFICATION_SERVICE);
  511. Intent i = new Intent();
  512. if (type == 1) {
  513. i.setClass(MainActivity.this, MainActivity.class);
  514. } else if (type == 2) {
  515. i.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  516. } else {
  517. i.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
  518. i.addCategory(Intent.CATEGORY_DEFAULT);
  519. i.setData(Uri.parse("package:" + MainActivity.this.getPackageName()));
  520. i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  521. i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
  522. i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
  523. }
  524. i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  525.  
  526. PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
  527.  
  528. Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  529.  
  530. NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);
  531. switch(type){
  532. case 1:
  533. builder.setTicker(getString(R.string.app_name));
  534. builder.setContentTitle(getString(R.string.loc_fail));
  535. builder.setContentText(getString(R.string.loc_fail_text));
  536. builder.setStyle(new NotificationCompat.BigTextStyle().bigText(getString(R.string.loc_fail_more)));
  537. builder.setVibrate(new long[]{350,350,350,350,350});
  538. builder.setSmallIcon(R.mipmap.ic_launcher);
  539. break;
  540.  
  541. case 2:
  542. builder.setTicker(getString(R.string.app_name));
  543. builder.setContentTitle(getString(R.string.app_name));
  544. builder.setContentText(getString(R.string.loc_perm_text));
  545. builder.setStyle(new NotificationCompat.BigTextStyle().bigText(getString(R.string.loc_perm_more)));
  546. builder.setVibrate(new long[]{350, 700, 350, 700, 350});
  547. builder.setSound(alarmSound);
  548. builder.setSmallIcon(R.mipmap.ic_launcher);
  549. break;
  550. }
  551. builder.setOngoing(false);
  552. builder.setAutoCancel(true);
  553. builder.setContentIntent(pendingIntent);
  554. builder.setWhen(when);
  555. builder.setContentIntent(pendingIntent);
  556. asw_notification_new = builder.getNotification();
  557. asw_notification.notify(id, asw_notification_new);
  558. }
  559.  
  560. //Checking if users allowed the requested permissions or not
  561. @Override
  562. public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults){
  563. switch (requestCode){
  564. case 1: {
  565. if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
  566. get_location();
  567. }else{
  568. show_notification(2, 2);
  569. Toast.makeText(MainActivity.this, R.string.loc_req, Toast.LENGTH_LONG).show();
  570. }
  571. }
  572. }
  573. }
  574.  
  575. //Action on back key tap/click
  576. @Override
  577. public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
  578. if (event.getAction() == KeyEvent.ACTION_DOWN) {
  579. switch (keyCode) {
  580. case KeyEvent.KEYCODE_BACK:
  581. if (asw_view.canGoBack()) {
  582. asw_view.goBack();
  583. } else {
  584. finish();
  585. }
  586. return true;
  587. }
  588. }
  589. return super.onKeyDown(keyCode, event);
  590. }
  591.  
  592. @Override
  593. protected void onStart() {
  594. super.onStart();
  595. }
  596.  
  597. @Override
  598. protected void onStop() {
  599. super.onStop();
  600. }
  601.  
  602. @Override
  603. public void onConfigurationChanged(Configuration newConfig) {
  604. super.onConfigurationChanged(newConfig);
  605. }
  606.  
  607. @Override
  608. protected void onSaveInstanceState(Bundle outState ){
  609. super.onSaveInstanceState(outState);
  610. asw_view.saveState(outState);
  611. }
  612.  
  613. @Override
  614. protected void onRestoreInstanceState(Bundle savedInstanceState){
  615. super.onRestoreInstanceState(savedInstanceState);
  616. asw_view.restoreState(savedInstanceState);
  617. }
  618. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement