]> git.hungrycats.org Git - xscreensaver/blobdiff - android/xscreensaver/src/org/jwz/xscreensaver/jwxyz.java
From https://www.jwz.org/xscreensaver/xscreensaver-6.09.tar.gz
[xscreensaver] / android / xscreensaver / src / org / jwz / xscreensaver / jwxyz.java
index 8ca6af40588b0fcbb8e8affa8d826f48aa0036d8..889040a4522b3715cdb6dc48f2f8af71fa6c5e6b 100644 (file)
@@ -1,5 +1,5 @@
 /* -*- Mode: java; indent-tabs-mode: nil; c-basic-offset: 2 -*-
- * xscreensaver, Copyright (c) 2016-2018 Jamie Zawinski <jwz@jwz.org>
+ * xscreensaver, Copyright © 2016-2021 Jamie Zawinski <jwz@jwz.org>
  *
  * Permission to use, copy, modify, distribute, and sell this software and its
  * documentation for any purpose is hereby granted without fee, provided that
@@ -57,6 +57,11 @@ import android.media.ExifInterface;
 import org.jwz.xscreensaver.TTFAnalyzer;
 import android.util.Log;
 import android.view.Surface;
+import android.Manifest;
+import android.support.v4.app.ActivityCompat;
+import android.support.v4.content.ContextCompat;
+import android.os.Build;
+import android.content.pm.PackageManager;
 
 public class jwxyz
   implements GestureDetector.OnGestureListener,
@@ -100,6 +105,8 @@ public class jwxyz
   public final static int FONT_FACE   = 1;
   public final static int FONT_RANDOM = 2;
 
+  public final static int MY_REQ_READ_EXTERNAL_STORAGE = 271828;
+
   private long nativeRunningHackPtr;
 
   private String hack;
@@ -253,9 +260,9 @@ public class jwxyz
           // LOG ("unparsable system font: %s", file);
         } else {
           name = mungeFontName (name);
-          if (! all_fonts.contains (name)) {
+          if (! all_fonts.contains (name.toLowerCase())) {
             // LOG ("system font \"%s\" %s", name, file);
-            all_fonts.put (name, name);
+            all_fonts.put (name.toLowerCase(), name);
           }
         }
       }
@@ -294,7 +301,7 @@ public class jwxyz
         tmpfile.delete();
 
         name = mungeFontName (name);
-        all_fonts.put (name, t);
+        all_fonts.put (name.toLowerCase(), t);
         // LOG ("asset font \"%s\" %s", name, fn);
       } catch (Exception e) {
         if (tmpfile != null) tmpfile.delete();
@@ -351,31 +358,41 @@ public class jwxyz
 
 
   // Parses "Native Font Name One 12, Native Font Name Two 14".
-  // Returns [ String name, Typeface ]
+  // Returns [ String name, Typeface ] or null.
   private Object[] parseNativeFont (String name) {
-    Object font2 = all_fonts.get (name);
-    if (font2 instanceof String)
+    Object font2 = all_fonts.get (name.toLowerCase());
+    if (font2 == null)
+      return null;
+    if (font2 instanceof String)  // Might be a Typeface or a String
       font2 = Typeface.create (name, Typeface.NORMAL);
     return new Object[] { name, (Typeface)font2 };
   }
 
 
   // Returns [ Paint paint, String family_name, Float ascent, Float descent ]
+  // or null.
   public Object[] loadFont(int mask, int traits, String name, int name_type,
                            float size) {
-    Object pair[];
+    Object pair[] = null;
 
-    if (name_type != FONT_RANDOM && name.equals("")) return null;
+    if (name_type != FONT_RANDOM && name.equals(""))
+      return null;
 
-    if (name_type == FONT_FACE) {
+    if (name_type != FONT_RANDOM)      // First try native or bundled names
       pair = parseNativeFont (name);
-    } else {
+
+    if (pair == null)                  // Then guess whether it's monospace
       pair = parseXLFD (mask, traits, name, name_type);
-    }
+
+    if (pair == null)
+      return null;
 
     String name2  = (String)   pair[0];
     Typeface font = (Typeface) pair[1];
 
+    if (font == null)
+      return null;
+
     size *= 2;
 
     String suffix = (font.isBold() && font.isItalic() ? " bold italic" :
@@ -627,6 +644,40 @@ public class jwxyz
   }
 
 
+  boolean havePermission(String permission) {
+
+        if (Build.VERSION.SDK_INT < 16) {
+            return true;
+        }
+
+        if (permissionGranted(permission)) {
+            return true;
+        }
+
+        return false;
+  }
+
+
+  private boolean permissionGranted(String permission) {
+        boolean check = ContextCompat.checkSelfPermission(app, permission) ==
+                PackageManager.PERMISSION_GRANTED;
+        return check;
+  }
+
+  public Object[] checkThenLoadRandomImage (int target_width, int target_height,
+                                   boolean rotate_p) {
+      // RES introduced in API 16
+      String permission = Manifest.permission.READ_EXTERNAL_STORAGE;
+
+        if (havePermission(permission)) {
+            return loadRandomImage(target_width,target_height,rotate_p);
+        } else {
+            return null;
+        }
+  }
+
+  // "getBitmap(ContentResolver,Uri) in Media has been deprecated"
+  @SuppressWarnings("deprecation")
   public Object[] loadRandomImage (int target_width, int target_height,
                                    boolean rotate_p) {
 
@@ -636,6 +687,9 @@ public class jwxyz
     ArrayList<String> imgs = new ArrayList<String>();
 
     ContentResolver cr = app.getContentResolver();
+
+    // "DATA in MediaColumns has been deprecated"
+    @SuppressWarnings("deprecation")
     String[] cols = { MediaColumns.DATA,
                       MediaColumns.MIME_TYPE,
                       MediaColumns.WIDTH,
@@ -691,6 +745,9 @@ public class jwxyz
 
     try {
       try {
+        // "getBitmap(ContentResolver,Uri) in Media has been deprecated"
+        // How do I do this for this block, instead of for the whole function?
+        // @SuppressWarnings("deprecation")
         bitmap = MediaStore.Images.Media.getBitmap (cr, uri);
       } catch (Exception e) {
         LOG ("image %s unloadable: %s", which, e.toString());