remove alert service

This commit is contained in:
Milos Kozak 2016-07-19 11:31:58 +02:00
parent 58c7ec077b
commit e00c01ebee
6 changed files with 21 additions and 272 deletions

View file

@ -65,9 +65,6 @@
android:name=".Services.DataService"
android:exported="false" />
<!-- Service showing alert on screen -->
<service
android:name=".Services.AlertService"
android:exported="false" />
<service
android:name=".plugins.DanaR.Services.ExecutionService"

View file

@ -1,16 +1,13 @@
package info.nightscout.androidaps;
import android.Manifest;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
@ -22,34 +19,13 @@ import com.squareup.otto.Subscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Iterator;
import info.nightscout.androidaps.Services.AlertService;
import info.nightscout.androidaps.events.EventAppExit;
import info.nightscout.androidaps.events.EventRefreshGui;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.plugins.Careportal.CareportalFragment;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment;
import info.nightscout.androidaps.plugins.DanaR.DanaRFragment;
import info.nightscout.androidaps.plugins.DanaR.Services.ExecutionService;
import info.nightscout.androidaps.plugins.Loop.LoopFragment;
import info.nightscout.androidaps.plugins.LowSuspend.LowSuspendFragment;
import info.nightscout.androidaps.plugins.MM640g.MM640gFragment;
import info.nightscout.androidaps.plugins.NSProfileViewer.NSProfileViewerFragment;
import info.nightscout.androidaps.plugins.OpenAPSMA.OpenAPSMAFragment;
import info.nightscout.androidaps.plugins.Overview.OverviewFragment;
import info.nightscout.androidaps.plugins.SafetyFragment.SafetyFragment;
import info.nightscout.androidaps.plugins.SimpleProfile.SimpleProfileFragment;
import info.nightscout.androidaps.plugins.SmsCommunicator.SmsCommunicatorFragment;
import info.nightscout.androidaps.plugins.SourceNSClient.SourceNSClientFragment;
import info.nightscout.androidaps.plugins.SourceXdrip.SourceXdripFragment;
import info.nightscout.androidaps.plugins.TempBasals.TempBasalsFragment;
import info.nightscout.androidaps.plugins.Treatments.TreatmentsFragment;
import info.nightscout.androidaps.plugins.VirtualPump.VirtualPumpFragment;
import info.nightscout.androidaps.receivers.KeepAliveReceiver;
import info.nightscout.androidaps.tabs.*;
import info.nightscout.androidaps.plugins.Objectives.ObjectivesFragment;
import info.nightscout.androidaps.tabs.SlidingTabLayout;
import info.nightscout.androidaps.tabs.TabPageAdapter;
import info.nightscout.utils.ImportExportPrefs;
import info.nightscout.utils.LocaleHelper;
@ -141,22 +117,22 @@ public class MainActivity extends AppCompatActivity {
ImportExportPrefs.verifyStoragePermissions(this);
ImportExportPrefs.importSharedPreferences(this);
break;
case R.id.nav_test_alarm:
final int REQUEST_CODE_ASK_PERMISSIONS = 2355;
int permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.SYSTEM_ALERT_WINDOW);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
// On Android 6 give permission for alarming in Settings -> Apps -> Draw over other apps
ActivityCompat.requestPermissions(
this,
new String[]{Manifest.permission.SYSTEM_ALERT_WINDOW},
REQUEST_CODE_ASK_PERMISSIONS
);
}
Intent alertServiceIntent = new Intent(getApplicationContext(), AlertService.class);
alertServiceIntent.putExtra("alertText", getString(R.string.nav_test_alert));
getApplicationContext().startService(alertServiceIntent);
break;
// case R.id.nav_test_alarm:
// final int REQUEST_CODE_ASK_PERMISSIONS = 2355;
// int permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.SYSTEM_ALERT_WINDOW);
// if (permission != PackageManager.PERMISSION_GRANTED) {
// // We don't have permission so prompt the user
// // On Android 6 give permission for alarming in Settings -> Apps -> Draw over other apps
// ActivityCompat.requestPermissions(
// this,
// new String[]{Manifest.permission.SYSTEM_ALERT_WINDOW},
// REQUEST_CODE_ASK_PERMISSIONS
// );
// }
// Intent alertServiceIntent = new Intent(getApplicationContext(), AlertService.class);
// alertServiceIntent.putExtra("alertText", getString(R.string.nav_test_alert));
// getApplicationContext().startService(alertServiceIntent);
// break;
case R.id.nav_exit:
log.debug("Exiting");
keepAliveReceiver.cancelAlarm(this);

View file

@ -1,152 +0,0 @@
package info.nightscout.androidaps.Services;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.PowerManager;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Calendar;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
public class AlertMessage {
private static Logger log = LoggerFactory.getLogger(AlertMessage.class);
private static boolean displayed = false;
private WindowManager mWindowManager;
private LinearLayout mLinLayout;
private WindowManager.LayoutParams mLinLayoutParams;
private TextView mFloatingTextView;
private Context mApplicationContext;
private Button mButtonDismis;
private static SoundPool mSoundPool;
private static int mSoundID;
private static int mPlayingId;
private Runnable mOnDismiss;
private String mAlertText = "Alarm";
PowerManager.WakeLock mWakeLock;
static {
mSoundPool = new SoundPool(1, AudioManager.STREAM_ALARM, 0);
mSoundID = mSoundPool.load(MainApp.instance().getApplicationContext(), R.raw.beep_beep, 1);
}
public AlertMessage(Context mApplicationContext) {
this.mApplicationContext = mApplicationContext;
PowerManager powerManager = (PowerManager) mApplicationContext.getSystemService(Context.POWER_SERVICE);
mWakeLock = powerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "AlertMessage");
}
public void showMessage() {
log.debug("showMessage() displayed:" + displayed);
if (displayed) {
return;
}
displayed = true;
mWakeLock.acquire();
mPlayingId = 0;
int hourOfDay = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
float volume = (hourOfDay > 11 && hourOfDay < 23) ? 0.03f : 1.0f;
do {
mPlayingId = mSoundPool.play(mSoundID, volume, volume, 0, -1, 1f);
log.debug("mSoundPool.play returned " + mPlayingId);
if (mPlayingId == 0) {
try {
synchronized (this) {
this.wait(100);
}
} catch (InterruptedException e) {
}
}
} while (mPlayingId == 0);
if (mWindowManager == null)
mWindowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
if (mLinLayout != null)
mWindowManager.removeViewImmediate(mLinLayout);
mLinLayout = new LinearLayout(getApplicationContext());
mLinLayoutParams = new WindowManager.LayoutParams();
mLinLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
mLinLayoutParams.format = 1;
mLinLayoutParams.flags =
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
WindowManager.LayoutParams.FLAG_FULLSCREEN;
mLinLayoutParams.width = 500;
mLinLayoutParams.height = 200;
mLinLayoutParams.gravity = Gravity.CENTER;
mLinLayout.setBackgroundColor(Color.argb(220, 255, 0, 0));
mWindowManager.addView(mLinLayout, mLinLayoutParams);
mLinLayout.setOrientation(LinearLayout.VERTICAL);
mFloatingTextView = new TextView(getApplicationContext());
mLinLayout.addView(mFloatingTextView);
mFloatingTextView.setText(mAlertText);
mFloatingTextView.setTypeface(Typeface.create("sans-serif-condensed", Typeface.NORMAL));
mFloatingTextView.setTextSize(24.0F);
mFloatingTextView.setGravity(Gravity.CENTER);
mFloatingTextView.setTextColor(Color.WHITE);
mButtonDismis = new Button(getApplicationContext());
mButtonDismis.setText(MainApp.sResources.getString(R.string.dismiss));
mButtonDismis.setTextSize(20.0F);
mButtonDismis.setTextAlignment(Button.TEXT_ALIGNMENT_CENTER);
mButtonDismis.setGravity(Gravity.CENTER);
mButtonDismis.setPadding(30, 20, 30, 20);
mLinLayout.addView(mButtonDismis);
mButtonDismis.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertMessage.this.dismis();
if (mOnDismiss != null) {
mOnDismiss.run();
}
}
});
}
public void dismis() {
mSoundPool.stop(mPlayingId);
if (mWindowManager == null)
mWindowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
if (mLinLayout != null)
mWindowManager.removeViewImmediate(mLinLayout);
displayed = false;
mWakeLock.release();
log.debug("dismis()");
}
private Context getApplicationContext() {
return mApplicationContext;
}
public void setText(String text) {
mAlertText = text;
}
public void setOnDismiss(Runnable runnable) {
mOnDismiss = runnable;
}
}

View file

@ -1,71 +0,0 @@
package info.nightscout.androidaps.Services;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.preference.PreferenceManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Date;
import info.nightscout.androidaps.Config;
public class AlertService extends Service {
private static Logger log = LoggerFactory.getLogger(AlertService.class);
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (Config.logFunctionCalls)
log.debug("onStartCommand");
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
long lastAlert = preferences.getLong("lastAlert", 0);
long currentTime = new Date().getTime();
//if (!PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean("masterSwitch", false)) {
// stopSelf(startId);
// log.debug("Alert posponed - master switch disabled");
//} else
//if ((currentTime - lastAlert) < 15 * 60 * 1000) {
// stopSelf(startId);
// log.debug("Alert posponed");
//} else
{
AlertMessage alert = new AlertMessage(getApplicationContext());
if (intent != null) {
String alertText = intent.getStringExtra("alertText");
if (alertText != null) {
alert.setText(alertText);
}
alert.setOnDismiss(new Runnable() {
@Override
public void run() {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putLong("lastAlert", new Date().getTime());
editor.commit();
AlertService.this.stopSelf();
}
});
alert.showMessage();
}
}
if (Config.logFunctionCalls)
log.debug("onStartCommand end");
return START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}

View file

@ -25,8 +25,8 @@ import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
@ -35,7 +35,6 @@ import info.nightscout.androidaps.db.Treatment;
import info.nightscout.androidaps.events.EventNewBG;
import info.nightscout.androidaps.events.EventNewBasalProfile;
import info.nightscout.androidaps.events.EventTreatmentChange;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment;
import info.nightscout.androidaps.plugins.Objectives.ObjectivesFragment;

View file

@ -41,7 +41,7 @@ public class KeepAliveReceiver extends BroadcastReceiver {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
danaRFragment.doConnect("KeepAlive");
danaRFragment.doConnect("KeepAlive"); // TODO: only if if last conn > 30 min
}
});
t.start();