-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainActivity.java
More file actions
570 lines (462 loc) · 24 KB
/
MainActivity.java
File metadata and controls
570 lines (462 loc) · 24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
package com.acecods.cscdemo;
import android.Manifest;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.DownloadManager;
import android.content.ClipData;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.print.PrintAttributes;
import android.print.PrintDocumentAdapter;
import android.print.PrintManager;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.DownloadListener;
import android.webkit.GeolocationPermissions;
import android.webkit.PermissionRequest;
import android.webkit.URLUtil;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private WebView webView;
private Button btnSave;
private static String file_type = "image/*";
private static final int MY_PERMISSION_REQUEST_CODE = 123;
private static final String TAG = MainActivity.class.getSimpleName();
private String cam_file_data = null; // for storing camera file information
private ValueCallback<Uri> file_data; // data/header received after file selection
private ValueCallback<Uri[]> file_path; // received file(s) temp. location
private final static int file_req_code = 1;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
super.onActivityResult(requestCode, resultCode, intent);
if(Build.VERSION.SDK_INT >= 21){
Uri[] results = null;
/*-- if file request cancelled; exited camera. we need to send null value to make future attempts workable --*/
if (resultCode == Activity.RESULT_CANCELED) {
if (requestCode == file_req_code) {
file_path.onReceiveValue(null);
return;
}
}
/*-- continue if response is positive --*/
if(resultCode== Activity.RESULT_OK){
if(requestCode == file_req_code){
if(null == file_path){
return;
}
ClipData clipData;
String stringData;
try {
clipData = intent.getClipData();
stringData = intent.getDataString();
}catch (Exception e){
clipData = null;
stringData = null;
}
if (clipData == null && stringData == null && cam_file_data != null) {
results = new Uri[]{Uri.parse(cam_file_data)};
}else{
if (clipData != null) { // checking if multiple files selected or not
final int numSelectedFiles = clipData.getItemCount();
results = new Uri[numSelectedFiles];
for (int i = 0; i < clipData.getItemCount(); i++) {
results[i] = clipData.getItemAt(i).getUri();
}
} else {
results = new Uri[]{Uri.parse(stringData)};
}
}
}
}
file_path.onReceiveValue(results);
file_path = null;
}else{
if(requestCode == file_req_code){
if(null == file_data) return;
Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
file_data.onReceiveValue(result);
file_data = null;
}
}
}
@SuppressLint({"SetJavaScriptEnabled", "WrongViewCast"})
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//////WEBVIEW//////
this.webView = (WebView) findViewById(R.id.webview);
this.btnSave = (Button) findViewById(R.id.btnSave);
webView.loadUrl("https://acecal.000webhostapp.com/goa/");
assert webView != null;
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setGeolocationEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.getSettings().setGeolocationDatabasePath( this.getFilesDir().getPath() );
//// INTERNAL AND EXTERNAL LINK HANDLER/////
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("acecal") && !url.contains("pdf")) {
view.loadUrl(url);
btnSave.setVisibility(View.GONE);
return true;
}
else if (url.contains("acecal") && url.contains("pdf")) {
view.loadUrl(url);
btnSave.setVisibility(View.VISIBLE);
return true;
}
else {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
}
return true;
}
/////////OFFLINE OR POOR SERVICE - LOADS HTML PAGE SAVED IN ASSETS FOLDER/////
public void onReceivedError(WebView webView, int i, String s, String s1) {
webView.loadUrl("file:///android_asset/error.html");
}
});
////////Geolocation Services - For getting Latitude and Longitude//////////
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
}, 0);
webView.setWebChromeClient(new WebChromeClient() {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void onPermissionRequest(final PermissionRequest request) {
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
request.grant(request.getResources());
}
});
}
public void onPermissionRequestCanceled(PermissionRequest request) {
Log.d(TAG, "onPermissionRequestCanceled");
}
/*-- handling input[type="file"] requests for android API 21+ --*/
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
if(file_permission() && Build.VERSION.SDK_INT >= 21) {
file_path = filePathCallback;
Intent takePictureIntent = null;
Intent takeVideoIntent = null;
boolean includeVideo = false;
boolean includePhoto = false;
/*-- checking the accept parameter to determine which intent(s) to include --*/
paramCheck:
for (String acceptTypes : fileChooserParams.getAcceptTypes()) {
String[] splitTypes = acceptTypes.split(", ?+"); // although it's an array, it still seems to be the whole value; split it out into chunks so that we can detect multiple values
for (String acceptType : splitTypes) {
switch (acceptType) {
case "*/*":
includePhoto = true;
break paramCheck;
case "image/*":
includePhoto = true;
break;
}
}
}
if (fileChooserParams.getAcceptTypes().length == 0) { //no `accept` parameter was specified, allow both photo and video
includePhoto = true;
// includeVideo = true;
}
if (includePhoto) {
takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(MainActivity.this.getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = create_image();
takePictureIntent.putExtra("PhotoPath", cam_file_data);
} catch (IOException ex) {
Log.e(TAG, "Image file creation failed", ex);
}
if (photoFile != null) {
cam_file_data = "file:" + photoFile.getAbsolutePath();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
} else {
cam_file_data = null;
takePictureIntent = null;
}
}
}
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
contentSelectionIntent.setType(file_type);
Intent[] intentArray;
if (takePictureIntent != null && takeVideoIntent != null) {
intentArray = new Intent[]{takePictureIntent, takeVideoIntent};
} else if (takePictureIntent != null) {
intentArray = new Intent[]{takePictureIntent};
} else if (takeVideoIntent != null) {
intentArray = new Intent[]{takeVideoIntent};
} else {
intentArray = new Intent[0];
}
Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
chooserIntent.putExtra(Intent.EXTRA_TITLE, "File chooser");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
startActivityForResult(chooserIntent, file_req_code);
return true;
} else {
return false;
}
}
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, false);
}
});
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
createWebPrintJob(webView);
} else {
}
}
});
webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setJavaScriptEnabled(true);
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
CookieManager.getInstance().setAcceptCookie(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
////////////DOWNLOAD MANAGER///////
webView.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent, String contentDescription,
String mimetype, long contentLength) {
/*
DownloadManager.Request
This class contains all the information necessary to request a new download.
The URI is the only required parameter. Note that the default download
destination is a shared volume where the system might delete your file
if it needs to reclaim space for system use. If this is a problem,
use a location on external storage (see setDestinationUri(Uri).
*/
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
/*
void allowScanningByMediaScanner ()
If the file to be downloaded is to be scanned by MediaScanner, this method
should be called before enqueue(Request) is called.
*/
request.allowScanningByMediaScanner();
/*
DownloadManager.Request setNotificationVisibility (int visibility)
Control whether a system notification is posted by the download manager
while this download is running or when it is completed. If enabled, the
download manager posts notifications about downloads through the system
NotificationManager. By default, a notification is shown only
when the download is in progress.
It can take the following values: VISIBILITY_HIDDEN, VISIBILITY_VISIBLE,
VISIBILITY_VISIBLE_NOTIFY_COMPLETED.
If set to VISIBILITY_HIDDEN, this requires the permission
android.permission.DOWNLOAD_WITHOUT_NOTIFICATION.
Parameters
visibility int : the visibility setting value
Returns
DownloadManager.Request this object
*/
request.setNotificationVisibility(
DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
/*
DownloadManager
The download manager is a system service that handles long-running HTTP
downloads. Clients may request that a URI be downloaded to a particular
destination file. The download manager will conduct the download in the
background, taking care of HTTP interactions and retrying downloads
after failures or across connectivity changes and system reboots.
*/
/*
String guessFileName (String url, String contentDisposition, String mimeType)
Guesses canonical filename that a download would have, using the URL
and contentDisposition. File extension, if not defined,
is added based on the mimetype
Parameters
url String : Url to the content
contentDisposition String : Content-Disposition HTTP header or null
mimeType String : Mime-type of the content or null
Returns
String : suggested filename
*/
String fileName = URLUtil.guessFileName(url,contentDescription,mimetype);
/*
DownloadManager.Request setDestinationInExternalPublicDir
(String dirType, String subPath)
Set the local destination for the downloaded file to a path within
the public external storage directory (as returned by
getExternalStoragePublicDirectory(String)).
The downloaded file is not scanned by MediaScanner. But it can be made
scannable by calling allowScanningByMediaScanner().
Parameters
dirType String : the directory type to pass to
getExternalStoragePublicDirectory(String)
subPath String : the path within the external directory, including
the destination filename
Returns
DownloadManager.Request this object
Throws
IllegalStateException : If the external storage directory cannot be
found or created.
*/
request.setDestinationInExternalPublicDir("/Aceplayx",fileName);
DownloadManager dManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
/*
long enqueue (DownloadManager.Request request)
Enqueue a new download. The download will start automatically once the
download manager is ready to execute it and connectivity is available.
Parameters
request DownloadManager.Request : the parameters specifying this download
Returns
long : an ID for the download, unique across the system. This ID is used
to make future calls related to this download.
*/
dManager.enqueue(request);
}
});
}
/*-- callback reporting if error occurs --*/
public class Callback extends WebViewClient{
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl){
Toast.makeText(getApplicationContext(), "Failed loading app!", Toast.LENGTH_SHORT).show();
}
}
/*-- checking and asking for required file permissions --*/
public boolean file_permission(){
if(Build.VERSION.SDK_INT >=23 && (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, 1);
return false;
}else{
return true;
}
}
/*-- creating new image file here --*/
private File create_image() throws IOException{
@SuppressLint("SimpleDateFormat") String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "img_"+timeStamp+"_";
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
return File.createTempFile(imageFileName,".jpg",storageDir);
}
/*-- creating new video file here --*/
private File create_video() throws IOException {
@SuppressLint("SimpleDateFormat")
String file_name = new SimpleDateFormat("yyyy_mm_ss").format(new Date());
String new_name = "file_"+file_name+"_";
File sd_directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
return File.createTempFile(new_name, ".3gp", sd_directory);
}
protected void checkPermission(){
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
if(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
if(shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)){
// show an alert dialog
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("Write external storage permission is required.");
builder.setTitle("Please grant permission");
builder.setPositiveButton((Html.fromHtml("<font color=black >OK</font>")), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ActivityCompat.requestPermissions(
MainActivity.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSION_REQUEST_CODE
);
}
});
builder.setNeutralButton((Html.fromHtml("<font color=black >Cancel</font>")),null);
AlertDialog dialog = builder.create();
dialog.show();
}else {
// Request permission
ActivityCompat.requestPermissions(
MainActivity.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSION_REQUEST_CODE
);
}
}else {
// Permission already granted
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){
switch(requestCode){
case MY_PERMISSION_REQUEST_CODE:{
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
// Permission granted
}else {
// Permission denied
}
}
}
}
private void createWebPrintJob(WebView webView) {
PrintManager printManager = (PrintManager) this
.getSystemService(Context.PRINT_SERVICE);
PrintDocumentAdapter printAdapter =
webView.createPrintDocumentAdapter();
String jobName = getString(R.string.app_name) + " Print Test";
if (printManager != null) {
printManager.print(jobName, printAdapter,
new PrintAttributes.Builder().build());
}
}
/////////////BACK BUTTON IS CLICKED//////
@Override
public void onBackPressed() {
if (webView.canGoBack()) {
String url = webView.getUrl();
if (url.contains("signin"))
webView.reload();
else
webView.goBack();
btnSave.setVisibility(View.GONE);
} else {
}
}
@Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
}
}