AlarmService

This commit is contained in:
Milos Kozak 2016-07-06 22:05:28 +02:00
parent c16ad7e37d
commit 7c46407f81
8 changed files with 264 additions and 22 deletions

View file

@ -2,10 +2,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="info.nightscout.androidaps">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
@ -32,6 +28,8 @@
</intent-filter>
</activity>
<activity android:name=".PreferencesActivity"></activity>
<!-- Receiver from NSClient -->
<receiver
android:name=".receivers.NSClientDataReceiver"
@ -65,8 +63,11 @@
<service
android:name=".Services.DataService"
android:exported="false" />
<!-- Service showing alarm on screen -->
<service
android:name=".Services.AlarmService"
android:exported="false" />
<activity android:name=".PreferencesActivity"></activity>
<meta-data
android:name="io.fabric.ApiKey"
android:value="59d462666c664c57b29e1d79ea123e01f8057cfa" />

View file

@ -1,8 +1,10 @@
package info.nightscout.androidaps;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
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;
@ -19,6 +21,7 @@ import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Iterator;
import info.nightscout.androidaps.Services.AlarmService;
import info.nightscout.androidaps.events.EventRefreshGui;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.plugins.Careportal.CareportalFragment;
@ -123,44 +126,53 @@ public class MainActivity extends AppCompatActivity {
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.nav_preferences: {
case R.id.nav_preferences:
Intent i = new Intent(getApplicationContext(), PreferencesActivity.class);
startActivity(i);
break;
}
case R.id.nav_resetdb:
MainApp.getDbHelper().resetDatabases();
break;
case R.id.en_lang: {
case R.id.en_lang:
LocaleHelper.setLocale(this, "en");
recreate();
break;
}
case R.id.cs_lang: {
case R.id.cs_lang:
LocaleHelper.setLocale(this, "cs");
recreate();
break;
}
case R.id.de_lang: {
case R.id.de_lang:
LocaleHelper.setLocale(this, "de");
recreate();
break;
}
case R.id.bg_lang: {
case R.id.bg_lang:
LocaleHelper.setLocale(this, "bg");
recreate();
break;
}
case R.id.nav_export: {
case R.id.nav_export:
ImportExportPrefs.verifyStoragePermissions(this);
ImportExportPrefs.exportSharedPreferences(this);
break;
}
case R.id.nav_import: {
case R.id.nav_import:
ImportExportPrefs.verifyStoragePermissions(this);
ImportExportPrefs.importSharedPreferences(this);
break;
}
case R.id.nav_testalarm:
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 alarmServiceIntent = new Intent(getApplicationContext(), AlarmService.class);
alarmServiceIntent.putExtra("alarmText",getString(R.string.testalarm));
getApplicationContext().startService(alarmServiceIntent);
break;
case R.id.nav_exit:
log.debug("Exiting");
//chancelAlarmManager();

View file

@ -0,0 +1,154 @@
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.util.Log;
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.Config;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
public class AlarmMessage {
private static Logger log = LoggerFactory.getLogger(AlarmMessage.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 mAlarmText = "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 AlarmMessage(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, "AlarmMessage");
}
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(mAlarmText);
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.resources.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) {
AlarmMessage.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) {
mAlarmText = text;
}
public void setOnDismiss(Runnable runnable) {
mOnDismiss = runnable;
}
}

View file

@ -0,0 +1,71 @@
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 AlarmService extends Service {
private static Logger log = LoggerFactory.getLogger(AlarmService.class);
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (Config.logFunctionCalls)
log.debug("onStartCommand");
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
long lastAlarm = preferences.getLong("lastAlarm", 0);
long currentTime = new Date().getTime();
//if (!PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean("masterSwitch", false)) {
// stopSelf(startId);
// log.debug("Alarm posponed - master switch disabled");
//} else
//if ((currentTime - lastAlarm) < 15 * 60 * 1000) {
// stopSelf(startId);
// log.debug("Alarm posponed");
//} else
{
AlarmMessage alarm = new AlarmMessage(getApplicationContext());
if (intent != null) {
String alarmText = intent.getStringExtra("alarmText");
if (alarmText != null) {
alarm.setText(alarmText);
}
alarm.setOnDismiss(new Runnable() {
@Override
public void run() {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putLong("lastAlarm", new Date().getTime());
editor.commit();
AlarmService.this.stopSelf();
}
});
alarm.showMessage();
}
}
if (Config.logFunctionCalls)
log.debug("onStartCommand end");
return START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}

View file

@ -10,9 +10,6 @@ import android.content.pm.PackageManager;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.support.v4.app.ActivityCompat;
import android.widget.Toast;
import com.crashlytics.android.Crashlytics;
import java.io.BufferedReader;
import java.io.File;

View file

@ -28,6 +28,9 @@
<item
android:id="@+id/bg_lang"
android:title="@string/bg_lang" />
<item
android:id="@+id/nav_testalarm"
android:title="@string/testalarm" />
<item
android:id="@+id/nav_exit"
android:title="@string/nav_exit" />

View file

@ -218,4 +218,6 @@
<string name="openapsma_maxiob_summary">Maximální nebolusové IOB, které může OpenAPS použít</string>
<string name="openapsma_targetrange_label">Cílový rozsah pro OpenAPS</string>
<string name="bg_lang">Bulgarian</string>
<string name="testalarm">Test alarmu</string>
<string name="dismiss">POTVRDIT</string>
</resources>

View file

@ -226,5 +226,7 @@
<string name="openapsma_maxbasal_title">Max Basal</string>
<string name="openapsma_maxbasaliob_title">Max Basal IOB</string>
<string name="bg_lang">Bulgarian</string>
<string name="testalarm">Alarm test</string>
<string name="dismiss">DISMISS</string>
</resources>