Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show Persistent notification only when connected to pu wifi #13

Merged
merged 2 commits into from
Oct 23, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

<application
android:allowBackup="true"
Expand Down
32 changes: 25 additions & 7 deletions app/src/main/java/org/developfreedom/logmein/LoginService.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;
import android.annotation.TargetApi;
import android.content.*;
import android.net.wifi.*;

import org.developfreedom.logmein.ui.MainActivity;
import org.developfreedom.logmein.ui.SettingsActivity;
Expand All @@ -46,6 +49,22 @@ public class LoginService extends Service {
private SharedPreferences preferences;
NetworkEngine networkEngine;
DatabaseEngine databaseEngine;
private final BroadcastReceiver receiver = new BroadcastReceiver() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look good for now. We'll implement our own receiver if we need more in future.

@Override
public void onReceive(Context context, Intent intent) {
WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
String SSID = "\"pu@campus\"";
if (!wifi.getConnectionInfo().getSSID().equalsIgnoreCase(SSID)) {
try {
mNotificationManager.cancelAll();
}catch(Exception e){
e.printStackTrace();
}
return;
}
showNotificationOrStop();
}
};

@Override
public void onCreate() {
Expand All @@ -56,15 +75,17 @@ public void onCreate() {
prefNeedPersistence = preferences.getBoolean(SettingsActivity.KEY_PERSISTENCE, SettingsActivity.DEFAULT_KEY_PERSISTENCE);
mNotificationManager = (NotificationManager) getSystemService(
NOTIFICATION_SERVICE);

showNotificationOrStop();
IntentFilter filter = new IntentFilter();
filter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
registerReceiver(receiver, filter);
// showNotificationOrStop();
Log.i("LoginService", "Login service created");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("LoginService", "performing onStartCommand");
showNotificationOrStop();
// showNotificationOrStop();
return 1;
}

Expand All @@ -91,10 +112,6 @@ public IBinder onBind(Intent intent) {
* Show a notification if possible else stop service
*/
void showNotificationOrStop() {
prefNeedPersistence = preferences.getBoolean(SettingsActivity.KEY_PERSISTENCE, SettingsActivity.DEFAULT_KEY_PERSISTENCE);
mNotificationManager = (NotificationManager) getSystemService(
NOTIFICATION_SERVICE);

//Only show expanding notification after version 16 i.e Jelly Bean
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
if (prefNeedPersistence) {
Expand All @@ -109,6 +126,7 @@ void showNotificationOrStop() {
/**
* Show a notification while this service is running.
*/
@TargetApi(16)
private void showPersistentNotification() {
int API = android.os.Build.VERSION.SDK_INT;//TODO: global
if (API < 16) return;
Expand Down