|
| 1 | +/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 | + |
| 3 | +/* |
| 4 | + Part of the Processing project - http://processing.org |
| 5 | +
|
| 6 | + Copyright (c) 2017 The Processing Foundation |
| 7 | +
|
| 8 | + This library is free software; you can redistribute it and/or |
| 9 | + modify it under the terms of the GNU Lesser General Public |
| 10 | + License version 2.1 as published by the Free Software Foundation. |
| 11 | +
|
| 12 | + This library is distributed in the hope that it will be useful, |
| 13 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | + Lesser General Public License for more details. |
| 16 | +
|
| 17 | + You should have received a copy of the GNU Lesser General |
| 18 | + Public License along with this library; if not, write to the |
| 19 | + Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
| 20 | + Boston, MA 02111-1307 USA |
| 21 | +*/ |
| 22 | + |
| 23 | +package processing.android; |
| 24 | + |
| 25 | +import java.util.concurrent.atomic.AtomicInteger; |
| 26 | +import android.annotation.SuppressLint; |
| 27 | +import android.os.Build; |
| 28 | +import android.util.DisplayMetrics; |
| 29 | +import android.view.Display; |
| 30 | +import android.view.View; |
| 31 | +import android.graphics.Point; |
| 32 | + |
| 33 | + |
| 34 | +/** |
| 35 | + * Compatibility utilities that work across versions of Android. Even though |
| 36 | + * the mode sets API level 17 (Android 4.2) as the minimum target, because the |
| 37 | + * core library could be used from another IDE and lower targets, then this |
| 38 | + * compatibility methods are still needed. |
| 39 | + */ |
| 40 | +public class CompatUtils { |
| 41 | + // Start at 15,000,000, taking into account the comment from Singed |
| 42 | + // http://stackoverflow.com/a/39307421 |
| 43 | + private static final AtomicInteger nextId = new AtomicInteger(15000000); |
| 44 | + |
| 45 | + |
| 46 | + /** |
| 47 | + * This method retrieves the "real" display metrics and size, without |
| 48 | + * subtracting any window decor or applying any compatibility scale factors. |
| 49 | + * @param display the Display object |
| 50 | + * @param metrics the metrics to retrieve |
| 51 | + * @param size the size to retrieve |
| 52 | + */ |
| 53 | + static public void getDisplayParams(Display display, |
| 54 | + DisplayMetrics metrics, Point size) { |
| 55 | + if (Build.VERSION_CODES.JELLY_BEAN_MR1 <= Build.VERSION.SDK_INT) { |
| 56 | + display.getRealMetrics(metrics); |
| 57 | + display.getRealSize(size); |
| 58 | + } if (Build.VERSION_CODES.ICE_CREAM_SANDWICH <= Build.VERSION.SDK_INT) { |
| 59 | + display.getMetrics(metrics); |
| 60 | + // Use undocumented methods getRawWidth, getRawHeight |
| 61 | + try { |
| 62 | + size.x = (Integer) Display.class.getMethod("getRawWidth").invoke(display); |
| 63 | + size.y = (Integer) Display.class.getMethod("getRawHeight").invoke(display); |
| 64 | + } catch (Exception e) { |
| 65 | + display.getSize(size); |
| 66 | + } |
| 67 | + } else { |
| 68 | + display.getMetrics(metrics); |
| 69 | + display.getSize(size); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + /** |
| 75 | + * This method generates a unique View ID's. Handles the lack of |
| 76 | + * View.generateViewId() in Android versions lower than 17, using a technique |
| 77 | + * based on fantouch's code at http://stackoverflow.com/a/21000252 |
| 78 | + * @return view ID |
| 79 | + */ |
| 80 | + @SuppressLint("NewApi") |
| 81 | + static public int getUniqueViewId() { |
| 82 | + if (Build.VERSION_CODES.JELLY_BEAN_MR1 < Build.VERSION.SDK_INT) { |
| 83 | + return View.generateViewId(); |
| 84 | + } else { |
| 85 | + for (;;) { |
| 86 | + final int result = nextId.get(); |
| 87 | + // aapt-generated IDs have the high byte nonzero; clamp to the range under that. |
| 88 | + int newValue = result + 1; |
| 89 | + if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0. |
| 90 | + if (nextId.compareAndSet(result, newValue)) { |
| 91 | + return result; |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments