Skip to content

Commit 7784fff

Browse files
committed
keep compatibility code in android package
1 parent 512bbce commit 7784fff

8 files changed

Lines changed: 132 additions & 58 deletions

File tree

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
}

core/src/processing/android/PFragment.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,11 @@ public PFragment(PApplet sketch) {
6565

6666

6767
public void initDimensions() {
68-
WindowManager wm = getActivity().getWindowManager();
69-
Display display = wm.getDefaultDisplay();
70-
7168
metrics = new DisplayMetrics();
72-
display.getRealMetrics(metrics);
73-
7469
size = new Point();
75-
display.getRealSize(size);
70+
WindowManager wm = getActivity().getWindowManager();
71+
Display display = wm.getDefaultDisplay();
72+
CompatUtils.getDisplayParams(display, metrics, size);
7673
}
7774

7875

core/src/processing/android/PWallpaper.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,11 @@ public class PWallpaper extends WallpaperService implements AppComponent {
3939

4040

4141
public void initDimensions() {
42-
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
43-
Display display = wm.getDefaultDisplay();
44-
4542
metrics = new DisplayMetrics();
46-
display.getRealMetrics(metrics);
47-
4843
size = new Point();
49-
display.getRealSize(size);
44+
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
45+
Display display = wm.getDefaultDisplay();
46+
CompatUtils.getDisplayParams(display, metrics, size);
5047
}
5148

5249

core/src/processing/android/PWatchFaceCanvas.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,11 @@ public class PWatchFaceCanvas extends CanvasWatchFaceService implements AppCompo
4949

5050

5151
public void initDimensions() {
52-
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
53-
Display display = wm.getDefaultDisplay();
54-
5552
metrics = new DisplayMetrics();
56-
display.getRealMetrics(metrics);
57-
5853
size = new Point();
59-
display.getRealSize(size);
54+
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
55+
Display display = wm.getDefaultDisplay();
56+
CompatUtils.getDisplayParams(display, metrics, size);
6057
}
6158

6259

core/src/processing/android/PWatchFaceGLES.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,11 @@ public class PWatchFaceGLES extends Gles2WatchFaceService implements AppComponen
4747

4848

4949
public void initDimensions() {
50-
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
51-
Display display = wm.getDefaultDisplay();
52-
5350
metrics = new DisplayMetrics();
54-
display.getRealMetrics(metrics);
55-
5651
size = new Point();
57-
display.getRealSize(size);
52+
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
53+
Display display = wm.getDefaultDisplay();
54+
CompatUtils.getDisplayParams(display, metrics, size);
5855
}
5956

6057

core/src/processing/android/PermissionRequestor.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
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+
123
package processing.android;
224

325
import android.app.Activity;

core/src/processing/android/ViewIdGenerator.java

Lines changed: 0 additions & 32 deletions
This file was deleted.

templates/AppActivity.java.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import android.widget.FrameLayout;
77
import android.support.v7.app.AppCompatActivity;
88

99
import processing.android.PFragment;
10-
import processing.android.ViewIdGenerator;
10+
import processing.android.CompatUtils;
1111
import processing.core.PApplet;
1212

1313
public class MainActivity extends AppCompatActivity {
@@ -17,7 +17,7 @@ public class MainActivity extends AppCompatActivity {
1717
protected void onCreate(Bundle savedInstanceState) {
1818
super.onCreate(savedInstanceState);
1919
FrameLayout frame = new FrameLayout(this);
20-
frame.setId(ViewIdGenerator.getUniqueId());
20+
frame.setId(CompatUtils.getUniqueViewId());
2121
setContentView(frame, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
2222
ViewGroup.LayoutParams.MATCH_PARENT));
2323

0 commit comments

Comments
 (0)