Skip to content

Commit a43374c

Browse files
committed
added ViewIdGenerator
1 parent 8565139 commit a43374c

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

core/src/processing/android/PermissionRequestor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.support.v4.app.ActivityCompat;
66
import android.support.v4.os.ResultReceiver;
77

8+
// A simple utility activity to request permissions in a service.
89
public class PermissionRequestor extends Activity {
910
public static final String KEY_RESULT_RECEIVER = "resultReceiver";
1011
public static final String KEY_PERMISSIONS = "permissions";
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package processing.android;
2+
3+
import java.util.concurrent.atomic.AtomicInteger;
4+
import android.annotation.SuppressLint;
5+
import android.os.Build;
6+
import android.view.View;
7+
8+
// An utility class to generate unique View ID's. Needed until minimum API level
9+
// in raised to 17, where we can just use View.generateViewId().
10+
// Largely based on fantouch code at http://stackoverflow.com/a/21000252
11+
public class ViewIdGenerator {
12+
// Start at 15,000,000, taking into account the comment from Singed
13+
// http://stackoverflow.com/a/39307421
14+
private static final AtomicInteger nextId = new AtomicInteger(15000000);
15+
16+
@SuppressLint("NewApi")
17+
public static int getUniqueId() {
18+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
19+
for (;;) {
20+
final int result = nextId.get();
21+
// aapt-generated IDs have the high byte nonzero; clamp to the range under that.
22+
int newValue = result + 1;
23+
if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
24+
if (nextId.compareAndSet(result, newValue)) {
25+
return result;
26+
}
27+
}
28+
} else {
29+
return View.generateViewId();
30+
}
31+
}
32+
}

templates/FragmentActivity.java.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package @@package_name@@;
22

33
import android.os.Bundle;
4-
import android.view.View;
54
import android.view.ViewGroup;
65
import android.widget.FrameLayout;
76
import android.support.v7.app.AppCompatActivity;
87

98
import processing.android.PFragment;
9+
import processing.android.ViewIdGenerator;
1010
import processing.core.PApplet;
1111

1212
public class MainActivity extends AppCompatActivity {
@@ -16,7 +16,7 @@ public class MainActivity extends AppCompatActivity {
1616
protected void onCreate(Bundle savedInstanceState) {
1717
super.onCreate(savedInstanceState);
1818
FrameLayout frame = new FrameLayout(this);
19-
frame.setId(View.generateViewId());
19+
frame.setId(ViewIdGenerator.getUniqueId());
2020
setContentView(frame, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
2121
ViewGroup.LayoutParams.MATCH_PARENT));
2222

0 commit comments

Comments
 (0)