Skip to content

Commit 5f62fdb

Browse files
committed
Move iOS native snippets into demo module
1 parent 135946c commit 5f62fdb

File tree

46 files changed

+1398
-829
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1398
-829
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.codename1.sms.intercept;
2+
3+
import android.Manifest;
4+
import android.content.IntentFilter;
5+
import com.codename1.impl.android.AndroidNativeUtil;
6+
7+
// tag::nativeSmsInterceptorImpl[]
8+
public class NativeSMSInterceptorImpl {
9+
private SMSListener smsListener;
10+
public void bindSMSListener() {
11+
if (AndroidNativeUtil.checkForPermission(Manifest.permission.RECEIVE_SMS, "We can automatically enter the SMS code for you")) {
12+
smsListener = new SMSListener();
13+
IntentFilter filter = new IntentFilter();
14+
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
15+
AndroidNativeUtil.getActivity().registerReceiver(smsListener, filter);
16+
}
17+
}
18+
19+
public void unbindSMSListener() {
20+
AndroidNativeUtil.getActivity().unregisterReceiver(smsListener);
21+
}
22+
23+
public boolean isSupported() {
24+
return true;
25+
}
26+
}
27+
// end::nativeSmsInterceptorImpl[]
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.codename1.sms.intercept;
2+
3+
import android.content.BroadcastReceiver;
4+
import android.content.Context;
5+
import android.content.Intent;
6+
import android.os.Bundle;
7+
import android.telephony.SmsMessage;
8+
import com.codename1.io.Log;
9+
10+
// tag::smsListener[]
11+
public class SMSListener extends BroadcastReceiver {
12+
13+
@Override
14+
public void onReceive(Context cntxt, Intent intent) {
15+
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
16+
Bundle bundle = intent.getExtras();
17+
SmsMessage[] msgs = null;
18+
if (bundle != null) {
19+
try {
20+
Object[] pdus = (Object[]) bundle.get("pdus");
21+
msgs = new SmsMessage[pdus.length];
22+
for (int i = 0; i < msgs.length; i++) {
23+
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
24+
String msgBody = msgs[i].getMessageBody();
25+
SMSCallback.smsReceived(msgBody);
26+
}
27+
} catch (Exception e) {
28+
Log.e(e);
29+
SMSCallback.smsReceiveError(e);
30+
}
31+
}
32+
}
33+
}
34+
}
35+
// end::smsListener[]
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.codenameone.developerguide.advancedtopics;
2+
3+
/**
4+
* Illustrates running code on Android's UI thread.
5+
*/
6+
public class AndroidThreadSnippets {
7+
8+
public void runAsync() {
9+
// tag::androidRunOnUiThread[]
10+
com.codename1.impl.android.AndroidNativeUtil.getActivity().runOnUiThread(new Runnable() {
11+
public void run() {
12+
// your native code here...
13+
}
14+
});
15+
// end::androidRunOnUiThread[]
16+
}
17+
18+
public void runBlocking() {
19+
// tag::androidRunOnUiThreadAndBlock[]
20+
com.codename1.impl.android.AndroidImplementation.runOnUiThreadAndBlock(new Runnable() {
21+
public void run() {
22+
// your native code here...
23+
}
24+
});
25+
// end::androidRunOnUiThreadAndBlock[]
26+
}
27+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.codenameone.developerguide.advancedtopics;
2+
3+
import com.codename1.impl.android.AndroidNativeUtil;
4+
5+
// tag::nativeCallsImpl[]
6+
class NativeCallsImpl {
7+
public void nativeMethod() {
8+
AndroidNativeUtil.getActivity().runOnUiThread(new Runnable() {
9+
public void run() {
10+
// ...
11+
}
12+
});
13+
}
14+
// ...
15+
}
16+
// end::nativeCallsImpl[]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.codenameone.developerguide.advancedtopics;
2+
3+
import android.view.View;
4+
5+
// tag::androidPeerImplementation[]
6+
class AndroidPeerImplementation {
7+
public View createPeer() {
8+
return null;
9+
}
10+
}
11+
// end::androidPeerImplementation[]
12+
13+
// tag::androidAllTypesSignature[]
14+
class AndroidAllTypesImplementation {
15+
public void test(byte param, boolean param1, char param2,
16+
short param3, int param4, long param5, float param6,
17+
double param7, String param8, byte[] param9,
18+
boolean[] param10, char[] param11, short[] param12,
19+
int[] param13, long[] param14, float[] param15,
20+
double[] param16, android.view.View param17) {
21+
}
22+
}
23+
// end::androidAllTypesSignature[]
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.codenameone.developerguide.advancedtopics;
2+
3+
import com.codename1.components.InfiniteProgress;
4+
import com.codename1.contacts.Contact;
5+
import com.codename1.ui.Display;
6+
import com.codename1.ui.Form;
7+
import com.codename1.ui.layouts.BoxLayout;
8+
import com.codename1.ui.list.MultiButton;
9+
import com.codename1.impl.android.AndroidNativeUtil;
10+
11+
/**
12+
* Snippets related to Android runtime permissions.
13+
*/
14+
public class PermissionSnippets {
15+
16+
public void showContactsWithPermission() {
17+
// tag::contactsPermission[]
18+
Form f = new Form("Contacts", BoxLayout.y());
19+
f.add(new InfiniteProgress());
20+
Display.getInstance().invokeAndBlock(() -> {
21+
Contact[] ct = Display.getInstance().getAllContacts(true, true, false, true, true, false);
22+
Display.getInstance().callSerially(() -> {
23+
f.removeAll();
24+
for (Contact c : ct) {
25+
MultiButton mb = new MultiButton(c.getDisplayName());
26+
mb.setTextLine2(c.getPrimaryPhoneNumber());
27+
f.add(mb);
28+
}
29+
f.revalidate();
30+
});
31+
});
32+
33+
f.show();
34+
// end::contactsPermission[]
35+
}
36+
37+
public void customizePermissionPrompt() {
38+
// tag::permissionPrompt[]
39+
Display.getInstance().setProperty(
40+
"android.permission.READ_CONTACTS",
41+
"MyCoolChatApp needs access to your contacts so we can show you which of your friends already have MyCoolChatApp installed");
42+
// end::permissionPrompt[]
43+
}
44+
45+
public void checkForPermission() {
46+
// tag::androidCheckForPermission[]
47+
if (!AndroidNativeUtil.checkForPermission(
48+
android.Manifest.permission.READ_PHONE_STATE,
49+
"This should be the description shown to the user...")) {
50+
// you didn't get the permission, you might want to return here
51+
}
52+
// you have the permission, do what you need
53+
// end::androidCheckForPermission[]
54+
}
55+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.codenameone.support.mobihelp;
2+
3+
import android.content.Context;
4+
import com.codename1.impl.android.AndroidNativeUtil;
5+
6+
// tag::mobihelpNativeImplContext[]
7+
class MobihelpNativeImpl {
8+
// tag::mobihelpNativeContext[]
9+
private static Context context() {
10+
return AndroidNativeUtil.getActivity().getApplicationContext();
11+
}
12+
// end::mobihelpNativeContext[]
13+
14+
// tag::mobihelpNativeClearUserData[]
15+
public void clearUserData() {
16+
com.freshdesk.mobihelp.Mobihelp.clearUserData(context());
17+
}
18+
// end::mobihelpNativeClearUserData[]
19+
20+
private static AndroidActivityWrapper activity() {
21+
return new AndroidActivityWrapper();
22+
}
23+
24+
// tag::mobihelpNativeShowSolutions[]
25+
public void showSolutions() {
26+
activity().runOnUiThread(new Runnable() {
27+
public void run() {
28+
com.freshdesk.mobihelp.Mobihelp.showSolutions(context());
29+
}
30+
});
31+
32+
}
33+
// end::mobihelpNativeShowSolutions[]
34+
35+
// tag::mobihelpNativeGetUnreadCountAsync[]
36+
public void getUnreadCountAsync(final int callbackId) {
37+
activity().runOnUiThread(new Runnable() {
38+
public void run() {
39+
com.freshdesk.mobihelp.Mobihelp.getUnreadCountAsync(context(), new com.freshdesk.mobihelp.UnreadUpdatesCallback() {
40+
public void onResult(com.freshdesk.mobihelp.MobihelpCallbackStatus status, Integer count) {
41+
MobihelpNativeCallback.fireUnreadUpdatesCallback(callbackId, status.ordinal(), count);
42+
}
43+
});
44+
}
45+
});
46+
47+
}
48+
// end::mobihelpNativeGetUnreadCountAsync[]
49+
}
50+
// end::mobihelpNativeImplContext[]
51+
52+
// Helper wrapper to simulate Android Activity runOnUiThread for compilation
53+
class AndroidActivityWrapper {
54+
void runOnUiThread(Runnable runnable) {
55+
runnable.run();
56+
}
57+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.freshdesk.mobihelp;
2+
3+
import android.content.Context;
4+
5+
public final class Mobihelp {
6+
public static void clearUserData(Context context) {
7+
// stub
8+
}
9+
10+
public static void showSolutions(Context context) {
11+
// stub
12+
}
13+
14+
public static void getUnreadCountAsync(Context context, UnreadUpdatesCallback callback) {
15+
// stub
16+
}
17+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.freshdesk.mobihelp;
2+
3+
public enum MobihelpCallbackStatus {
4+
SUCCESS,
5+
FAILURE
6+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.freshdesk.mobihelp;
2+
3+
public interface UnreadUpdatesCallback {
4+
void onResult(MobihelpCallbackStatus status, Integer count);
5+
}

0 commit comments

Comments
 (0)