Skip to content

Commit 06eba48

Browse files
committed
hi-dpi handling for permission selector
1 parent fcd8333 commit 06eba48

File tree

1 file changed

+28
-158
lines changed

1 file changed

+28
-158
lines changed

src/processing/mode/android/Permissions.java

Lines changed: 28 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,21 @@ public class Permissions extends JFrame {
4444
static final String GUIDE_URL =
4545
"https://developer.android.com/training/articles/security-tips.html#Permissions";
4646

47-
static final int BORDER_HORIZ = 5;
48-
static final int BORDER_VERT = 3;
49-
47+
static final int BORDER_HORIZ = Toolkit.zoom(5);
48+
static final int BORDER_VERT = Toolkit.zoom(3);
49+
static final int CELL_HEIGHT = Toolkit.zoom(20);
50+
static final int BORDER = Toolkit.zoom(13);
51+
static final int TEXT_WIDTH = Toolkit.zoom(400);
52+
static final int TEXT_HEIGHT = Toolkit.zoom(80);
53+
static final int URL_WIDTH = Toolkit.zoom(400);
54+
static final int URL_HEIGHT = Toolkit.zoom(30);
55+
static final int DESC_WIDTH = Toolkit.zoom(400);
56+
static final int DESC_HEIGHT = Toolkit.zoom(50);
57+
static final int GAP = Toolkit.zoom(8);
58+
5059
JScrollPane permissionScroller;
5160
JList<JCheckBox> permissionList;
5261
JLabel descriptionLabel;
53-
// JTextArea descriptionLabel;
54-
55-
// Editor editor;
5662
Sketch sketch;
5763

5864
int appComp;
@@ -61,43 +67,12 @@ public class Permissions extends JFrame {
6167

6268

6369
public Permissions(Sketch sketch, int appComp, File modeFolder) {
64-
//public Permissions(Editor editor) {
6570
super("Android Permissions Selector");
6671
this.appComp = appComp;
6772
this.sketch = sketch;
6873
this.modeFolder = modeFolder;
69-
// this.editor = editor;
70-
71-
// XMLElement xml =
7274

7375
permissionList = new CheckBoxList();
74-
// permissionList.addMouseListener(new MouseAdapter() {
75-
// public void mousePressed(MouseEvent e) {
76-
// if (isEnabled()) {
77-
// int index = permissionList.locationToIndex(e.getPoint());
78-
// if (index == -1) {
79-
// descriptionLabel.setText("");
80-
// } else {
81-
//// descriptionLabel.setText("<html>" + description[index] + "</html>");
82-
// descriptionLabel.setText(description[index]);
83-
// }
84-
// }
85-
// }
86-
// });
87-
88-
// ListSelectionModel lsm = permissionList.getSelectionModel();
89-
// lsm.addListSelectionListener(new ListSelectionListener() {
90-
// public void valueChanged(ListSelectionEvent e) {
91-
//// ListSelectionModel lsm = (ListSelectionModel) e.getSource();
92-
// int index = e.getFirstIndex();
93-
// if (index == -1) {
94-
// descriptionLabel.setText("");
95-
// } else {
96-
// descriptionLabel.setText("<html>" + description[index] + "</html>");
97-
//// descriptionLabel.setText(description[index]);
98-
// }
99-
// }
100-
// });
10176
permissionList.addListSelectionListener(new ListSelectionListener() {
10277
public void valueChanged(ListSelectionEvent e) {
10378
if (e.getValueIsAdjusting() == false) {
@@ -106,16 +81,11 @@ public void valueChanged(ListSelectionEvent e) {
10681
descriptionLabel.setText("");
10782
} else {
10883
descriptionLabel.setText("<html>" + description[index] + "</html>");
109-
//descriptionLabel.setText(description[index]);
11084
}
11185
}
11286
}
11387
});
114-
// permissionList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
115-
// permissionList.setFixedCellWidth(300);
116-
// int h = permissionList.getFixedCellHeight();
117-
// permissionList.setFixedCellHeight(h + 8);
118-
permissionList.setFixedCellHeight(20);
88+
permissionList.setFixedCellHeight(CELL_HEIGHT);
11989
permissionList.setBorder(new EmptyBorder(BORDER_VERT, BORDER_HORIZ,
12090
BORDER_VERT, BORDER_HORIZ));
12191

@@ -129,10 +99,7 @@ public void valueChanged(ListSelectionEvent e) {
12999
new JScrollPane(permissionList,
130100
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
131101
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
132-
// permissionList.setVisibleRowCount(20);
133102
permissionList.setVisibleRowCount(12);
134-
// permissionList.setPreferredSize(new Dimension(400, 300));
135-
// permissionsScroller.setPreferredSize(new Dimension(400, 300));
136103
permissionList.addKeyListener(new KeyAdapter() {
137104
public void keyTyped(KeyEvent e) {
138105
if (e.getKeyChar() == ' ') {
@@ -146,14 +113,9 @@ public void keyTyped(KeyEvent e) {
146113
});
147114

148115
Container outer = getContentPane();
149-
// outer.setLayout(new BorderLayout());
150-
151-
// JPanel pain = new JPanel();
152-
Box pain = Box.createVerticalBox();
153-
pain.setBorder(new EmptyBorder(13, 13, 13, 13));
154-
// outer.add(pain, BorderLayout.CENTER);
155-
outer.add(pain);
156-
// pain.setLayout(new BoxLayout(pain, BoxLayout.Y_AXIS));
116+
Box vbox = Box.createVerticalBox();
117+
vbox.setBorder(new EmptyBorder(BORDER, BORDER, BORDER, BORDER));
118+
outer.add(vbox);
157119

158120
String labelText =
159121
"<html>" +
@@ -162,104 +124,41 @@ public void keyTyped(KeyEvent e) {
162124
"or make phone calls. When installing your application,\n" +
163125
"users will be asked whether they want to allow such access.</html>";
164126
String urlText = "<html>More about permissions can be found " +
165-
"<a href=\"" + GUIDE_URL + "\">here</a>.</html>";
166-
167-
// "<html>" +
168-
// "Android applications must specifically ask for permission\n" +
169-
// "to do things like connect to the internet, write a file,\n" +
170-
// "or make phone calls. When installing your application,\n" +
171-
// "users will be asked whether they want to allow such access.\n" +
172-
// "More about permissions can be found " +
173-
// "<a href=\"" + GUIDE_URL + "\">here</a>.</body></html>";
174-
// JTextArea textarea = new JTextArea(labelText);
175-
// JTextArea textarea = new JTextArea(5, 40);
176-
// textarea.setText(labelText);
127+
"<a href=\"" + GUIDE_URL + "\">here</a>.</html>";
177128
JLabel textarea = new JLabel(labelText);
178129
JLabel urlarea = new JLabel(urlText);
179-
// JLabel textarea = new JLabel(labelText) {
180-
// public Dimension getPreferredSize() {
181-
// return new Dimension(400, 100);
182-
// }
183-
// public Dimension getMinimumSize() {
184-
// return getPreferredSize();
185-
// }
186-
// public Dimension getMaximumSize() {
187-
// return getPreferredSize();
188-
// }
189-
// };
190-
textarea.setPreferredSize(new Dimension(400, 80));
191-
urlarea.setPreferredSize(new Dimension(400, 30));
130+
textarea.setPreferredSize(new Dimension(TEXT_WIDTH, TEXT_HEIGHT));
131+
urlarea.setPreferredSize(new Dimension(URL_WIDTH, URL_HEIGHT));
192132
urlarea.addMouseListener(new MouseAdapter() {
193133
public void mouseClicked(MouseEvent e) {
194134
Platform.openURL(GUIDE_URL);
195135
}
196136
});
197137
urlarea.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
198-
//textarea.setHorizontalAlignment(SwingConstants.LEFT);
199138
textarea.setAlignmentX(LEFT_ALIGNMENT);
200139
urlarea.setAlignmentX(LEFT_ALIGNMENT);
201-
202-
// textarea.setBorder(new EmptyBorder(13, 8, 13, 8));
203-
204-
// textarea.setBackground(null);
205-
// textarea.setBackground(Color.RED);
206-
// textarea.setEditable(false);
207-
// textarea.setHighlighter(null);
208-
// textarea.setFont(new Font("Dialog", Font.PLAIN, 12));
209-
pain.add(textarea);
210-
pain.add(urlarea);
211-
// textarea.setForeground(Color.RED);
212-
// pain.setBackground(Color.GREEN);
213-
214-
// permissionList.setEnabled(false);
140+
vbox.add(textarea);
141+
vbox.add(urlarea);
215142

216143
permissionScroller.setAlignmentX(LEFT_ALIGNMENT);
217-
pain.add(permissionScroller);
218-
// pain.add(permissionList);
219-
pain.add(Box.createVerticalStrut(8));
144+
vbox.add(permissionScroller);
145+
vbox.add(Box.createVerticalStrut(GAP));
220146

221-
// descriptionLabel = new JTextArea(4, 10);
222147
descriptionLabel = new JLabel();
223-
// descriptionLabel = new JLabel() {
224-
// public Dimension getPreferredSize() {
225-
// return new Dimension(400, 100);
226-
// }
227-
// public Dimension getMinimumSize() {
228-
// return new Dimension(400, 100);
229-
// }
230-
// public Dimension getMaximumSize() {
231-
// return new Dimension(400, 100);
232-
// }
233-
// };
234-
descriptionLabel.setPreferredSize(new Dimension(400, 50));
148+
descriptionLabel.setPreferredSize(new Dimension(DESC_WIDTH, DESC_HEIGHT));
235149
descriptionLabel.setVerticalAlignment(SwingConstants.TOP);
236150
descriptionLabel.setAlignmentX(LEFT_ALIGNMENT);
237-
pain.add(descriptionLabel);
238-
pain.add(Box.createVerticalStrut(8));
151+
vbox.add(descriptionLabel);
152+
vbox.add(Box.createVerticalStrut(GAP));
239153

240154
JPanel buttons = new JPanel();
241-
// buttons.setPreferredSize(new Dimension(400, 35));
242-
// JPanel buttons = new JPanel() {
243-
// public Dimension getPreferredSize() {
244-
// return new Dimension(400, 35);
245-
// }
246-
// public Dimension getMinimumSize() {
247-
// return new Dimension(400, 35);
248-
// }
249-
// public Dimension getMaximumSize() {
250-
// return new Dimension(400, 35);
251-
// }
252-
// };
253-
254-
// Box buttons = Box.createHorizontalBox();
255155
buttons.setAlignmentX(LEFT_ALIGNMENT);
256156
JButton okButton = new JButton("OK");
257157
Dimension dim = new Dimension(Toolkit.getButtonWidth(),
258-
okButton.getPreferredSize().height);
158+
Toolkit.zoom(okButton.getPreferredSize().height));
259159
okButton.setPreferredSize(dim);
260160
okButton.addActionListener(new ActionListener() {
261161
public void actionPerformed(ActionEvent e) {
262-
//PApplet.println(getSelections());
263162
saveSelections();
264163
setVisible(false);
265164
}
@@ -278,15 +177,12 @@ public void actionPerformed(ActionEvent e) {
278177
// think different, biznatchios!
279178
if (Platform.isMacOS()) {
280179
buttons.add(cancelButton);
281-
// buttons.add(Box.createHorizontalStrut(8));
282180
buttons.add(okButton);
283181
} else {
284182
buttons.add(okButton);
285-
// buttons.add(Box.createHorizontalStrut(8));
286183
buttons.add(cancelButton);
287184
}
288-
// buttons.setMaximumSize(new Dimension(300, buttons.getPreferredSize().height));
289-
pain.add(buttons);
185+
vbox.add(buttons);
290186

291187
JRootPane root = getRootPane();
292188
root.setDefaultButton(okButton);
@@ -356,21 +252,6 @@ public String getMenuTitle() {
356252
}
357253

358254

359-
// public void init(Editor editor) {
360-
// this.editor = editor;
361-
// }
362-
363-
364-
// public void run() {
365-
// // parse the manifest file here and figure out what permissions are set
366-
// Manifest mf = new Manifest(editor);
367-
// setSelections(mf.getPermissions());
368-
//
369-
// // show the window and get to work
370-
// setVisible(true);
371-
// }
372-
373-
374255
/**
375256
* Created by running the parse.py script on a saved copy
376257
* https://developer.android.com/reference/android/Manifest.permission.html
@@ -590,25 +471,14 @@ public Component getListCellRendererComponent(JList<? extends JCheckBox> list,
590471
JCheckBox checkbox,
591472
int index, boolean isSelected,
592473
boolean cellHasFocus) {
593-
// checkbox.setBorder(new EmptyBorder(13, 5, 3, 5)); // trying again
594474
checkbox.setBackground(isSelected ? getSelectionBackground() : getBackground());
595475
checkbox.setForeground(isSelected ? getSelectionForeground() : getForeground());
596-
//checkbox.setEnabled(isEnabled());
597476
checkbox.setEnabled(list.isEnabled());
598477
checkbox.setFont(getFont());
599478
checkbox.setFocusPainted(false);
600479
checkbox.setBorderPainted(true);
601480
checkbox.setBorder(isSelected ? UIManager.getBorder("List.focusCellHighlightBorder") : noFocusBorder);
602481
return checkbox;
603482
}
604-
605-
// @Override
606-
// public Component getListCellRendererComponent(JList<? extends JCheckBox> list,
607-
// JCheckBox value, int index,
608-
// boolean isSelected,
609-
// boolean cellHasFocus) {
610-
// // TODO Auto-generated method stub
611-
// return null;
612-
// }
613483
}
614484
}

0 commit comments

Comments
 (0)