forked from blackberry/Samples-for-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocialScreen.java
More file actions
218 lines (189 loc) · 7.23 KB
/
Copy pathSocialScreen.java
File metadata and controls
218 lines (189 loc) · 7.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
* Copyright (c) 2012 Research In Motion Limited.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package mypackage;
import net.rim.blackberry.api.sendmenu.SendCommand;
import net.rim.blackberry.api.sendmenu.SendCommandContextKeys;
import net.rim.blackberry.api.sendmenu.SendCommandException;
import net.rim.blackberry.api.sendmenu.SendCommandRepository;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.decor.BorderFactory;
import net.rim.device.api.ui.picker.FilePicker;
import org.json.me.JSONException;
import org.json.me.JSONObject;
/**
* A class extending the MainScreen class, which provides default standard
* behavior for BlackBerry GUI applications.
*/
public final class SocialScreen extends MainScreen {
/** Figured out by experimentation with SendCommand.getId(). */
private final String TWITTER_TEXT_ID = "Twitter_text";
private final String TWITTER_PATH_ID = "Twitter_path";
private final String FACEBOOK_TEXT_ID = "Facebook_text";
private final String FACEBOOK_PATH_ID = "Facebook_path";
private ButtonField shareTextButton;
private EditField textField;
private ButtonField filePickButton;
private SendCommand[] commandsAll;
private SendCommand[] commands;
/**
* Creates a new MyScreen object
*/
public SocialScreen() {
setBorder(BorderFactory.createBitmapBorder(new XYEdges(9, 9, 9, 9), Bitmap.getBitmapResource("border.png")));
setTitle(new LabelField("(: The Social App :)", DrawStyle.HCENTER|Field.FIELD_HCENTER));
filePickButton = new ButtonField("Photo Share", Field.FIELD_HCENTER) {
protected boolean navigationClick(int status, int time) {
FilePicker picker = FilePicker.getInstance();
picker.setFilter(".jpg:.png");
picker.setView(FilePicker.VIEW_PICTURES);
String path = picker.show();
if (path != null) {
// Creating the data context as a JSONObject
JSONObject context = new JSONObject();
try {
context.put(SendCommandContextKeys.PATH, path);
/**
* Since we are sharing a file path, you CANNOT add a
* SUBJECT or TEXT type data. Only PATH seems to be
* allowed.
*/
} catch (JSONException e) {
}
/**
* Getting the available SendCommand(s) for our SendCommand
* type TYPE_TEXT by providing our data context. The 3rd
* parameter is a boolean that indicates whether you want
* all results or just the results that are possible based on the context.
* Ideally this boolean param would be set to false,
* but there seems to be a bug with BBM as it throws an
* exception during the query. As a workaround, I am getting
* all first and then filtering out the ones I dont need.
*/
commandsAll = SendCommandRepository.getInstance().get(SendCommand.TYPE_PATH, context, true);
commands = new SendCommand[2];
for (int i = 0; i < commandsAll.length; i++) {
if (commandsAll[i].getId().equals(TWITTER_PATH_ID)) {
commands[0] = commandsAll[i];
}
if (commandsAll[i].getId().equals(FACEBOOK_PATH_ID)) {
commands[1] = commandsAll[i];
}
}
// Displays our SendCommands to the user.
displayCommands();
}
return true;
}
public int getPreferredWidth() {
return Display.getWidth()/2;
}
};
add(filePickButton);
add(new SeparatorField());
add(new SeparatorField());
add(new SeparatorField());
// An EditField to let the user type in the text to share.
textField = new EditField("", "Share this please.");
textField.setBorder(BorderFactory.createRoundedBorder(new XYEdges(3, 3, 3, 3)));
// A button that initiates the Text Share
shareTextButton = new ButtonField("Text Share", Field.FIELD_HCENTER) {
protected boolean navigationClick(int arg0, int arg1) {
// Creating the data context as a JSONObject
JSONObject context = new JSONObject();
try {
context.put(SendCommandContextKeys.TEXT, textField.getText());
/**
* For text sharing, you can also add a subject as follows. This is useful for sharing via email.
* context.put(SendCommandContextKeys.SUBJECT, "your subject");
*/
} catch (JSONException e) {
}
commandsAll = SendCommandRepository.getInstance().get(SendCommand.TYPE_TEXT, context, true);
commands = new SendCommand[2];
for (int i = 0; i < commandsAll.length; i++) {
if (commandsAll[i].getId().equals(TWITTER_TEXT_ID)) {
commands[0] = commandsAll[i];
}
if (commandsAll[i].getId().equals(FACEBOOK_TEXT_ID)) {
commands[1] = commandsAll[i];
}
}
// Displays our SendCommands to the user.
displayCommands();
return true;
}
public int getPreferredWidth() {
return Display.getWidth()/2;
}
};
add(textField);
add(shareTextButton);
}
/**
* Displays our SendCommands to the user. Note that you can also use the
* prebuilt SendCommandScreen or SendCommandMenu to show the SendCommands.
* But I wanted to demo how you can add the SendCommands in a Screen that
* you own.
*
*/
private void displayCommands() {
SendDialog dialog = new SendDialog(Dialog.D_OK, "Lets be social!", Dialog.OK, null);
if(commands[0]!=null){
dialog.add(new ButtonField("Twitter") {
protected boolean navigationClick(int status, int time) {
try {
commands[0].run();
} catch (SendCommandException e) {
SendDialog.inform(Dialog.D_OK, e.toString(), Dialog.OK, null);
}
UiApplication.getUiApplication().popScreen(getScreen());
return true;
}
public int getPreferredWidth() {
return Display.getWidth();
}
});
}
if(commands[1]!=null){
dialog.add(new ButtonField("Facebook") {
protected boolean navigationClick(int status, int time) {
try {
commands[1].run();
} catch (SendCommandException e) {
SendDialog.inform(Dialog.D_OK, e.toString(), Dialog.OK, null);
}
UiApplication.getUiApplication().popScreen(getScreen());
return true;
}
public int getPreferredWidth() {
return Display.getWidth();
}
});
}
UiApplication.getUiApplication().pushScreen(dialog);
}
}