Skip to content

Commit 6bf11c0

Browse files
author
Devendra
committed
merging ZD-3230, fixing merging conflicts
2 parents 898b867 + 7d20684 commit 6bf11c0

6 files changed

Lines changed: 54 additions & 3 deletions

File tree

‎android/examples/PubnubExample/AndroidManifest.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@
2424
<uses-permission android:name="android.permission.INTERNET"/>
2525
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
2626
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
27+
<uses-permission android:name="android.permission.WAKE_LOCK"/>
2728

2829
</manifest>

‎android/examples/PubnubExample/res/menu/activity_main.xml‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,7 @@
1414
<item android:id="@+id/option13" android:title="@string/option13" android:titleCondensed="@string/option13"></item>
1515
<item android:id="@+id/option14" android:title="@string/option14" android:titleCondensed="@string/option14"></item>
1616
<item android:id="@+id/option15" android:title="@string/option15" android:titleCondensed="@string/option15"></item>
17-
<item android:id="@+id/option16" android:title="@string/option16" android:titleCondensed="@string/option16"></item>
18-
</menu>
17+
<item android:id="@+id/option16" android:title="@string/option16" android:titleCondensed="@string/option16"></item>
18+
<item android:id="@+id/option17" android:title="@string/option17" android:titleCondensed="@string/option17"></item>
19+
<item android:id="@+id/option18" android:title="@string/option18" android:titleCondensed="@string/option18"></item>
20+
</menu>

‎android/examples/PubnubExample/res/values/strings.xml‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
<string name="option14">Set Subscribe Timeout</string>
3636
<string name="option15">Set Non Subscribe Timeout</string>
3737
<string name="option16">Set Window Interval</string>
38+
<string name="option17">Acquire Partial Wake Lock</string>
39+
<string name="option18">Release Partial Wake Lock</string>
40+
<string name="option19">Check Wake Lock Status</string>
3841

3942

40-
</resources>
43+
</resources>

‎android/examples/PubnubExample/src/com/pubnub/examples/pubnubExample/MainActivity.java‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import android.content.IntentFilter;
1515
import android.net.ConnectivityManager;
1616
import android.os.Bundle;
17+
import android.os.PowerManager;
1718
import android.text.InputType;
1819
import android.util.Log;
1920
import android.view.Menu;
@@ -34,6 +35,7 @@ public class MainActivity extends Activity {
3435
*/
3536

3637
Pubnub pubnub = new Pubnub("demo", "demo", "", false);
38+
PowerManager.WakeLock wl = null;
3739

3840
private void notifyUser(Object message) {
3941
try {
@@ -88,6 +90,8 @@ public void onReceive(Context arg0, Intent intent) {
8890
}
8991

9092
}, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
93+
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
94+
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "PubnubDemoConsole");
9195

9296
}
9397

@@ -166,12 +170,35 @@ public boolean onOptionsItemSelected(MenuItem item) {
166170

167171
case R.id.option16:
168172
setWindowInterval();
173+
acquirePartialWakeLock();
174+
return true;
175+
176+
case R.id.option17:
177+
releasePartialWakeLock();
178+
return true;
179+
180+
case R.id.option18:
181+
checkWakeLockStatus();
169182
return true;
170183
default:
171184
return super.onOptionsItemSelected(item);
172185
}
173186
}
174187

188+
private void checkWakeLockStatus() {
189+
notifyUser("Partial Wake Lock : " + wl.isHeld());
190+
}
191+
192+
private void releasePartialWakeLock() {
193+
if (wl != null) wl.release();
194+
notifyUser("Partial Wake Lock : " + wl.isHeld());
195+
}
196+
197+
private void acquirePartialWakeLock() {
198+
if (wl != null) wl.acquire();
199+
notifyUser("Partial Wake Lock : " + wl.isHeld());
200+
}
201+
175202
private void setNonSubscribeTimeout() {
176203

177204
AlertDialog.Builder builder = new AlertDialog.Builder(this);

‎android/examples/SubscribeAtBoot/AndroidManifest.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,6 @@
3535

3636
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
3737
<uses-permission android:name="android.permission.INTERNET"/>
38+
<uses-permission android:name="android.permission.WAKE_LOCK"/>
3839

3940
</manifest>

‎android/examples/SubscribeAtBoot/src/com/pubnub/examples/subscribeAtBoot/PubnubService.java‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
import android.os.Handler;
44
import android.os.Message;
5+
import android.os.PowerManager;
56

67
import com.pubnub.api.Callback;
78
import com.pubnub.api.Pubnub;
89
import com.pubnub.api.PubnubException;
910

1011

1112
import android.app.Service;
13+
import android.content.Context;
1214
import android.content.Intent;
1315
import android.os.IBinder;
1416
import android.util.Log;
@@ -18,6 +20,7 @@ public class PubnubService extends Service {
1820

1921
String channel = "hello_world";
2022
Pubnub pubnub = new Pubnub("demo", "demo", false);
23+
PowerManager.WakeLock wl = null;
2124

2225
private final Handler handler = new Handler() {
2326
public void handleMessage(Message msg) {
@@ -55,6 +58,14 @@ private void notifyUser(Object message) {
5558
public void onCreate() {
5659
super.onCreate();
5760
Toast.makeText(this, "PubnubService created...", Toast.LENGTH_LONG).show();
61+
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
62+
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SubscribeAtBoot");
63+
if (wl != null) {
64+
wl.acquire();
65+
Log.i("PUBNUB", "Partial Wake Lock : " + wl.isHeld());
66+
Toast.makeText(this, "Partial Wake Lock : " + wl.isHeld(), Toast.LENGTH_LONG).show();
67+
}
68+
5869
Log.i("PUBNUB", "PubnubService created...");
5970
try {
6071
pubnub.subscribe(new String[] {channel}, new Callback() {
@@ -84,6 +95,12 @@ public void errorCallback(String channel, Object message) {
8495
@Override
8596
public void onDestroy() {
8697
super.onDestroy();
98+
if (wl != null) {
99+
wl.release();
100+
Log.i("PUBNUB", "Partial Wake Lock : " + wl.isHeld());
101+
Toast.makeText(this, "Partial Wake Lock : " + wl.isHeld(), Toast.LENGTH_LONG).show();
102+
wl = null;
103+
}
87104
Toast.makeText(this, "PubnubService destroyed...", Toast.LENGTH_LONG).show();
88105
}
89106

0 commit comments

Comments
 (0)