Proyectos de Subversion LeadersLinked - Android

Rev

Rev 5 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 5 Rev 10
Línea 24... Línea 24...
24
import android.net.ConnectivityManager;
24
import android.net.ConnectivityManager;
25
import android.net.NetworkInfo;
25
import android.net.NetworkInfo;
26
import android.net.Uri;
26
import android.net.Uri;
27
import android.os.Build;
27
import android.os.Build;
28
import android.os.Bundle;
28
import android.os.Bundle;
29
import android.os.Environment;
-
 
30
import android.provider.MediaStore;
-
 
31
import android.util.Log;
29
import android.util.Log;
32
import android.view.KeyEvent;
30
import android.view.KeyEvent;
33
import android.view.View;
31
import android.view.View;
34
import android.webkit.JavascriptInterface;
32
import android.webkit.JavascriptInterface;
35
import android.webkit.ValueCallback;
33
import android.webkit.ValueCallback;
36
import android.webkit.WebChromeClient;
34
import android.webkit.WebChromeClient;
37
import android.webkit.WebSettings;
35
import android.webkit.WebSettings;
38
import android.webkit.WebView;
36
import android.webkit.WebView;
39
import android.webkit.WebViewClient;
37
import android.webkit.WebViewClient;
Línea 40... Línea -...
40
 
-
 
41
import java.io.File;
-
 
42
import java.io.IOException;
-
 
43
import java.text.SimpleDateFormat;
38
 
44
import java.util.ArrayList;
-
 
Línea 45... Línea 39...
45
import java.util.Date;
39
import java.util.ArrayList;
46
 
-
 
47
public class MainActivity extends AppCompatActivity {
40
 
48
 
-
 
49
    private TouchyWebView mWebView = null;
-
 
50
 
41
public class MainActivity extends AppCompatActivity {
51
 
-
 
52
    private SwipeRefreshLayout mSwipeRefreshLayout;
42
    private TouchyWebView mWebView = null;
53
 
-
 
54
    private static String file_type = "*/*";
-
 
55
    private String cam_file_data = null;
43
    private SwipeRefreshLayout mSwipeRefreshLayout;
56
    private ValueCallback<Uri> file_data;
-
 
57
    private ValueCallback<Uri[]> file_path;
-
 
58
    private final static int file_req_code = 1;
44
    private static final String file_type = "*/*";
59
 
-
 
60
    private final static String _URL = "https://stanging.leaderslinked.com";
45
    private ValueCallback<Uri[]> file_path;
Línea 61... Línea 46...
61
 
46
    private final static String _URL = BuildConfig.BASE_URL;
62
    private ActivityResultLauncher<Intent> fileChooserLauncher;
47
    private ActivityResultLauncher<Intent> fileChooserLauncher;
63
 
48
 
64
    @RequiresApi(api = Build.VERSION_CODES.M)
49
    @RequiresApi(api = Build.VERSION_CODES.M)
65
    @Override
50
    @Override
Línea 66... Línea -...
66
    protected void onCreate(Bundle savedInstanceState) {
-
 
67
        super.onCreate(savedInstanceState);
51
    protected void onCreate(Bundle savedInstanceState) {
68
        setContentView(R.layout.activity_main);
52
        super.onCreate(savedInstanceState);
69
 
53
        setContentView(R.layout.activity_main);
70
 
54
 
71
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.swipeRefreshLayout), (v, insets) -> {
55
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.swipeRefreshLayout), (v, insets) -> {
Línea 81... Línea 65...
81
 
65
 
82
 
66
 
83
        fileChooserLauncher = registerForActivityResult(
67
        fileChooserLauncher = registerForActivityResult(
84
                new ActivityResultContracts.StartActivityForResult(),
68
                new ActivityResultContracts.StartActivityForResult(),
85
                result -> {
-
 
86
                    // For Android 5.0 (Lollipop) and above
69
                result -> {
87
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
-
 
88
                        if (file_path == null) {
-
 
89
                            return;
-
 
90
                        }
-
 
91
                        Uri[] uris = WebChromeClient.FileChooserParams.parseResult(result.getResultCode(), result.getData());
-
 
92
                        if (uris == null) {
-
 
93
                            if (cam_file_data != null) {
-
 
94
                                uris = new Uri[]{Uri.parse(cam_file_data)};
-
 
95
                            }
-
 
96
                        }
-
 
97
                        file_path.onReceiveValue(uris);
-
 
98
                        file_path = null;
-
 
99
                    } else { // For older versions
70
                    // Para selección de archivos de galería
100
                        if (file_data == null) {
-
 
101
                            return;
-
 
102
                        }
-
 
103
                        Uri resultUri = (result.getData() == null || result.getResultCode() != RESULT_OK) ? null : result.getData().getData();
-
 
104
                        file_data.onReceiveValue(resultUri);
71
                    if (file_path == null) {
-
 
72
                        return;
-
 
73
                    }
105
                        file_data = null;
74
                    Uri[] uris = WebChromeClient.FileChooserParams.parseResult(result.getResultCode(), result.getData());
106
                    }
75
                    file_path.onReceiveValue(uris);
Línea 107... Línea 76...
107
                    cam_file_data = null;
76
                    file_path = null;
Línea 108... Línea 77...
108
                });
77
                });
109
 
78
 
110
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
79
        mSwipeRefreshLayout = findViewById(R.id.swipeRefreshLayout);
111
 
80
 
112
        mSwipeRefreshLayout.setOnChildScrollUpCallback(new SwipeRefreshLayout.OnChildScrollUpCallback() {
81
        mSwipeRefreshLayout.setOnChildScrollUpCallback(new SwipeRefreshLayout.OnChildScrollUpCallback() {
113
            @Override
82
            @Override
Línea 114... Línea -...
114
            public boolean canChildScrollUp(@NonNull SwipeRefreshLayout parent, @Nullable View child) {
-
 
115
                return mWebView.canScrollVertically(-1);
-
 
116
            }
-
 
117
        });
83
            public boolean canChildScrollUp(@NonNull SwipeRefreshLayout parent, @Nullable View child) {
118
 
84
                return mWebView.canScrollVertically(-1);
119
 
85
            }
120
 
86
        });
121
 
87
 
Línea 208... Línea 174...
208
 
174
 
209
        mWebView.setWebChromeClient(new WebChromeClient()
175
        mWebView.setWebChromeClient(new WebChromeClient()
210
        {
176
        {
Línea 211... Línea 177...
211
            public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
177
            public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
212
 
178
 
213
                if(file_permission() && Build.VERSION.SDK_INT >= 21) {
-
 
214
                    file_path = filePathCallback;
-
 
215
                    Intent takePictureIntent = null;
-
 
216
                    Intent takeVideoIntent = null;
-
 
217
 
-
 
218
                    boolean includeVideo = false;
-
 
219
                    boolean includePhoto = false;
-
 
220
 
-
 
221
                    paramCheck:
-
 
222
                    for (String acceptTypes : fileChooserParams.getAcceptTypes()) {
-
 
223
                        String[] splitTypes = acceptTypes.split(", ?+");
-
 
224
                        for (String acceptType : splitTypes) {
-
 
225
                            switch (acceptType) {
-
 
226
                                case "*/*":
-
 
227
                                    includePhoto = true;
-
 
228
                                    includeVideo = true;
-
 
229
                                    break paramCheck;
-
 
230
                                case "image/*":
-
 
231
                                    includePhoto = true;
-
 
232
                                    break;
-
 
233
                                case "video/*":
-
 
234
                                    includeVideo = true;
-
 
235
                                    break;
-
 
236
                            }
-
 
237
                        }
-
 
238
                    }
-
 
239
 
-
 
240
                    if (fileChooserParams.getAcceptTypes().length == 0) {
-
 
241
                        includePhoto = true;
-
 
242
                        includeVideo = true;
-
 
243
                    }
-
 
244
 
-
 
245
                    if (includePhoto) {
-
 
246
                        takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
-
 
247
                        if (takePictureIntent.resolveActivity(MainActivity.this.getPackageManager()) != null) {
-
 
248
                            File photoFile = null;
-
 
249
                            try {
-
 
250
                                photoFile = create_image();
-
 
251
                                takePictureIntent.putExtra("PhotoPath", cam_file_data);
-
 
252
                            } catch (IOException ex) {
-
 
253
                                Log.e("MainClass" , "Image file creation failed", ex);
-
 
254
                            }
-
 
255
                            if (photoFile != null) {
-
 
256
                                cam_file_data = "file:" + photoFile.getAbsolutePath();
-
 
257
                                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
-
 
258
                            } else {
-
 
259
                                cam_file_data = null;
-
 
260
                                takePictureIntent = null;
-
 
261
                            }
-
 
262
                        }
-
 
263
                    }
-
 
264
 
-
 
265
                    if (includeVideo) {
-
 
266
                        takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
-
 
267
                        if (takeVideoIntent.resolveActivity(MainActivity.this.getPackageManager()) != null) {
-
 
268
                            File videoFile = null;
-
 
269
                            try {
-
 
270
                                videoFile = create_video();
-
 
271
                            } catch (IOException ex) {
-
 
272
                                Log.e("MainClass" , "Video file creation failed", ex);
-
 
273
                            }
-
 
274
                            if (videoFile != null) {
-
 
275
                                cam_file_data = "file:" + videoFile.getAbsolutePath();
-
 
276
                                takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(videoFile));
-
 
277
                            } else {
-
 
278
                                cam_file_data = null;
-
 
279
                                takeVideoIntent = null;
-
 
280
                            }
-
 
Línea -... Línea 179...
-
 
179
                if(file_permission()) {
281
                        }
180
                    file_path = filePathCallback;
282
                    }
181
 
283
 
182
                    // Solo permitir selección de galería/archivos
284
                    Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
-
 
285
                    contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
-
 
286
                    contentSelectionIntent.setType(file_type);
-
 
287
 
-
 
288
 
183
                    Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
289
                    Intent[] intentArray;
-
 
290
                    if (takePictureIntent != null && takeVideoIntent != null) {
-
 
291
                        intentArray = new Intent[]{takePictureIntent, takeVideoIntent};
-
 
292
                    } else if (takePictureIntent != null) {
-
 
293
                        intentArray = new Intent[]{takePictureIntent};
-
 
294
                    } else if (takeVideoIntent != null) {
-
 
295
                        intentArray = new Intent[]{takeVideoIntent};
-
 
Línea 296... Línea 184...
296
                    } else {
184
                    contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
297
                        intentArray = new Intent[0];
185
                    contentSelectionIntent.setType(file_type);
298
                    }
186
                    contentSelectionIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
299
 
-
 
-
 
187
 
300
                    Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
188
                    Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
301
                    chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
189
                    chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
302
                    chooserIntent.putExtra(Intent.EXTRA_TITLE, "File chooser");
190
                    chooserIntent.putExtra(Intent.EXTRA_TITLE, "Seleccionar archivo");
303
                    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
191
                    
304
                    fileChooserLauncher.launch(chooserIntent);
192
                    fileChooserLauncher.launch(chooserIntent);
305
                    return true;
193
                    return true;
306
                } else {
194
                } else {
Línea 307... Línea 195...
307
                    return false;
195
                    return false;
308
                }
196
                }
309
            }
197
            }
310
        });
198
        });
311
 
199
 
312
        // Verificar conectividad de red
200
        // Verify network connection
313
        if (isNetworkAvailable()) {
201
        if (isNetworkAvailable()) {
314
            Log.d("WebView", "Network available, proceeding to load URL");
202
            Log.d("WebView", "Network available, proceeding to load URL");
315
            // Limpiar completamente el caché antes de cargar
203
            // Clear cache before load
316
            clearWebViewCache();
204
            clearWebViewCache();
Línea 317... Línea 205...
317
            mWebView.loadUrl(_URL);
205
            mWebView.loadUrl(_URL);
Línea 325... Línea 213...
325
        mWebView.clearCache(true);
213
        mWebView.clearCache(true);
326
        mWebView.clearFormData();
214
        mWebView.clearFormData();
327
        mWebView.clearHistory();
215
        mWebView.clearHistory();
328
        mWebView.clearSslPreferences();
216
        mWebView.clearSslPreferences();
Línea 329... Línea 217...
329
        
217
        
330
        // Limpiar cookies
-
 
331
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
218
        // Clear cookies
332
            android.webkit.CookieManager.getInstance().removeAllCookies(null);
219
        android.webkit.CookieManager.getInstance().removeAllCookies(null);
333
            android.webkit.CookieManager.getInstance().flush();
-
 
334
        } else {
-
 
335
            android.webkit.CookieManager.getInstance().removeAllCookie();
-
 
336
        }
220
        android.webkit.CookieManager.getInstance().flush();
337
        
221
 
338
        Log.d("WebView", "Cache, cookies and data cleared completely");
222
        Log.d("WebView", "Cache, cookies and data cleared completely");
Línea 339... Línea 223...
339
    }
223
    }
340
    
224
    
Línea 385... Línea 269...
385
        int checkPermissionExternal = PackageManager.PERMISSION_GRANTED;
269
        int checkPermissionExternal = PackageManager.PERMISSION_GRANTED;
386
        int checkPermissionReadMediaAudio = PackageManager.PERMISSION_GRANTED;
270
        int checkPermissionReadMediaAudio = PackageManager.PERMISSION_GRANTED;
387
        int checkPermissionReadMediaVideo = PackageManager.PERMISSION_GRANTED;
271
        int checkPermissionReadMediaVideo = PackageManager.PERMISSION_GRANTED;
388
        int checkPermissionReadMediaImages = PackageManager.PERMISSION_GRANTED;
272
        int checkPermissionReadMediaImages = PackageManager.PERMISSION_GRANTED;
Línea 389... Línea -...
389
 
-
 
390
        int checkPermissionCamera = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
-
 
391
 
273
 
392
        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
274
        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
393
            checkPermissionExternal = ContextCompat.checkSelfPermission(this, WRITE_EXTERNAL_STORAGE);
275
            checkPermissionExternal = ContextCompat.checkSelfPermission(this, WRITE_EXTERNAL_STORAGE);
394
        } else {
276
        } else {
395
            checkPermissionReadMediaAudio = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_MEDIA_AUDIO);
277
            checkPermissionReadMediaAudio = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_MEDIA_AUDIO);
396
            checkPermissionReadMediaVideo = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_MEDIA_VIDEO);
278
            checkPermissionReadMediaVideo = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_MEDIA_VIDEO);
397
            checkPermissionReadMediaImages = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_MEDIA_IMAGES);
279
            checkPermissionReadMediaImages = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_MEDIA_IMAGES);
Línea 398... Línea 280...
398
        }
280
        }
399
 
281
 
400
 
282
 
401
        boolean ok = checkPermissionCamera == PackageManager.PERMISSION_GRANTED;
283
        boolean ok = true;
402
        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
284
        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
403
            ok = ok && checkPermissionExternal == PackageManager.PERMISSION_GRANTED;
285
            ok = checkPermissionExternal == PackageManager.PERMISSION_GRANTED;
404
        } else {
286
        } else {
405
            ok = ok && checkPermissionReadMediaAudio == PackageManager.PERMISSION_GRANTED;
287
            ok = checkPermissionReadMediaAudio == PackageManager.PERMISSION_GRANTED;
Línea 406... Línea 288...
406
            ok = ok && checkPermissionReadMediaVideo == PackageManager.PERMISSION_GRANTED;
288
            ok = ok && checkPermissionReadMediaVideo == PackageManager.PERMISSION_GRANTED;
Línea 407... Línea 289...
407
            ok = ok && checkPermissionReadMediaImages == PackageManager.PERMISSION_GRANTED;
289
            ok = ok && checkPermissionReadMediaImages == PackageManager.PERMISSION_GRANTED;
Línea 408... Línea -...
408
        }
-
 
409
 
-
 
410
        if(!ok) {
-
 
411
 
-
 
412
            ArrayList<String> newPermissionList = new ArrayList<>();
290
        }
413
 
291
 
414
            if(checkPermissionCamera != PackageManager.PERMISSION_GRANTED) {
292
        if(!ok) {
415
                newPermissionList.add(Manifest.permission.CAMERA);
293
 
416
            }
294
            ArrayList<String> newPermissionList = new ArrayList<>();
Línea 434... Línea 312...
434
 
312
 
435
 
313
 
436
 
314
 
437
            String permissions[] = newPermissionList.toArray(new String[newPermissionList.size()]);
315
            String permissions[] = newPermissionList.toArray(new String[newPermissionList.size()]);
438
            ActivityCompat.requestPermissions(MainActivity.this, permissions, 1);
316
            ActivityCompat.requestPermissions(MainActivity.this, permissions, 1);
439
            return true;
317
            return false; // No abrir selector hasta que se concedan permisos
440
        } else {
318
        } else {
Línea -... Línea 319...
-
 
319
            return true; // Permisos concedidos, permitir abrir selector
-
 
320
        }
-
 
321
    }
-
 
322
 
-
 
323
    @Override
-
 
324
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
441
            return false;
325
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
442
        }
326
        
443
    }
327
        if (requestCode == 1) {
-
 
328
            boolean allPermissionsGranted = true;
-
 
329
            for (int result : grantResults) {
-
 
330
                if (result != PackageManager.PERMISSION_GRANTED) {
-
 
331
                    allPermissionsGranted = false;
-
 
332
                    break;
444
 
333
                }
-
 
334
            }
-
 
335
            
445
    private File create_image() throws IOException {
336
            if (allPermissionsGranted) {
-
 
337
                Log.d("MainActivity", "Todos los permisos han sido concedidos");
-
 
338
                // Los permisos han sido concedidos, el usuario puede intentar nuevamente abrir el selector
-
 
339
            } else {
446
        @SuppressLint("SimpleDateFormat") String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
340
                Log.d("MainActivity", "Algunos permisos fueron denegados");
Línea 447... Línea -...
447
        String imageFileName = "img_"+timeStamp+"_";
-
 
448
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
-
 
449
        return File.createTempFile(imageFileName,".jpg",storageDir);
-
 
450
    }
-
 
451
 
-
 
452
    private File create_video() throws IOException {
-
 
453
        @SuppressLint("SimpleDateFormat")
-
 
Línea 454... Línea 341...
454
        String file_name = new SimpleDateFormat("yyyy_mm_ss").format(new Date());
341
                // Algunos permisos fueron denegados