Skip to content

Commit

Permalink
Alarm: Screen on and unlock the phone
Browse files Browse the repository at this point in the history
  • Loading branch information
deiteris committed Mar 20, 2017
1 parent caee759 commit 643ebb6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />

<application
android:allowBackup="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ public void onToggleClicked(View view) {
Log.d(TAG, "Legacy API functions have been executed");
setTimeLegacy(calendar);
}
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Log.d(TAG, "Alarm On");
} else {
AlarmReceiver.stopRingtone(this);
Expand All @@ -78,13 +77,15 @@ public void onToggleClicked(View view) {
public void setTimeLegacy(Calendar calendar) {
calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentHour());
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Toast.makeText(this, "Alarm has been set for " + alarmTimePicker.getCurrentHour() + " hour(s) " + alarmTimePicker.getCurrentHour() + " minute(s)", Toast.LENGTH_SHORT).show();
}

@TargetApi(Build.VERSION_CODES.M)
public void setTime(Calendar calendar) {
calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getHour());
calendar.set(Calendar.MINUTE, alarmTimePicker.getMinute());
alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Toast.makeText(this, "Alarm has been set for " + alarmTimePicker.getHour() + " hour(s) " + alarmTimePicker.getMinute() + " minute(s)", Toast.LENGTH_SHORT).show();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public void onHandleIntent(Intent intent) {
sendNotification(getString(R.string.incoming_call));
Intent launch = new Intent(this, LaunchActivity.class);
startActivity(launch);
AlarmReceiver.completeWakefulIntent(intent);
}

private void sendNotification(String msg) {
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/com/example/yink/amadeus/LaunchActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

Expand Down Expand Up @@ -68,11 +70,14 @@ protected void onCreate(Bundle savedInstanceState) {
pendingIntent = PendingIntent.getBroadcast(LaunchActivity.this, AlarmActivity.alarmCode, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationManager = (NotificationManager) getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
final Window win = getWindow();
if (!isAppInstalled(LaunchActivity.this, "com.google.android.googlequicksearchbox")) {
status.setText(R.string.google_app_error);
}
if (AlarmReceiver.isPlaying()) {
status.setText(R.string.incoming_call);
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}

connect.setImageResource(R.drawable.connect_unselect);
Expand Down Expand Up @@ -113,6 +118,8 @@ public void onCompletion(MediaPlayer mp) {
AlarmReceiver.stopRingtone(LaunchActivity.this);
notificationManager.cancel(1);
alarmManager.cancel(pendingIntent);
win.clearFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
win.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

Intent intent = new Intent(LaunchActivity.this, MainActivity.class);
startActivity(intent);
Expand All @@ -128,6 +135,8 @@ public void onClick(View view) {
AlarmReceiver.stopRingtone(getApplicationContext());
notificationManager.cancel(1);
alarmManager.cancel(pendingIntent);
win.clearFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
win.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
Expand All @@ -153,10 +162,14 @@ protected void attachBaseContext(Context newBase) {
@Override
protected void onDestroy() {
super.onDestroy();
final Window win = getWindow();

if (m != null)
m.release();
AlarmReceiver.stopRingtone(LaunchActivity.this);
notificationManager.cancel(1);
win.clearFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
win.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
aniHandle.removeCallbacks(aniRunnable);
}

Expand Down

0 comments on commit 643ebb6

Please sign in to comment.