216 lines
9.3 KiB
Java
216 lines
9.3 KiB
Java
package com.example.yink.amadeus;
|
|
|
|
import android.app.AlarmManager;
|
|
import android.app.NotificationManager;
|
|
import android.app.PendingIntent;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.content.SharedPreferences;
|
|
import android.content.pm.PackageManager;
|
|
import android.media.MediaPlayer;
|
|
import android.os.Bundle;
|
|
import android.os.Handler;
|
|
import android.os.Vibrator;
|
|
import android.preference.PreferenceManager;
|
|
import android.support.v4.app.NotificationCompat;
|
|
import android.support.v4.app.TaskStackBuilder;
|
|
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;
|
|
|
|
public class LaunchActivity extends AppCompatActivity {
|
|
|
|
private ImageView connect, cancel, logo;
|
|
private TextView status;
|
|
private Boolean isPressed = false;
|
|
private MediaPlayer m;
|
|
private Handler aniHandle = new Handler();
|
|
private AlarmManager alarmManager;
|
|
private NotificationManager notificationManager;
|
|
private Vibrator v;
|
|
|
|
private int i = 0;
|
|
|
|
Runnable aniRunnable = new Runnable() {
|
|
public void run() {
|
|
final int DURATION = 20;
|
|
if (i < 39) {
|
|
i += 1;
|
|
String imgName = "logo" + Integer.toString(i);
|
|
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());
|
|
logo.setImageDrawable((ContextCompat.getDrawable(LaunchActivity.this, id)));
|
|
aniHandle.postDelayed(this, DURATION);
|
|
}
|
|
}
|
|
};
|
|
|
|
private static boolean isAppInstalled(Context context, String packageName) {
|
|
try {
|
|
context.getPackageManager().getApplicationInfo(packageName, 0);
|
|
return true;
|
|
} catch (PackageManager.NameNotFoundException e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
setContentView(R.layout.activity_launch);
|
|
connect = (ImageView) findViewById(R.id.imageView_connect);
|
|
cancel = (ImageView) findViewById(R.id.imageView_cancel);
|
|
status = (TextView) findViewById(R.id.textView_call);
|
|
logo = (ImageView) findViewById(R.id.imageView_logo);
|
|
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
|
|
v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
|
|
Intent alarmIntent = new Intent(LaunchActivity.this, AlarmReceiver.class);
|
|
final PendingIntent pendingIntent = PendingIntent.getBroadcast(LaunchActivity.this, AlarmActivity.ALARM_ID, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
notificationManager = (NotificationManager) getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
|
|
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
|
|
final Window win = getWindow();
|
|
|
|
aniHandle.post(aniRunnable);
|
|
|
|
if (!isAppInstalled(LaunchActivity.this, "com.google.android.googlequicksearchbox")) {
|
|
status.setText(R.string.google_app_error);
|
|
}
|
|
|
|
if (AlarmReceiver.isPlaying()) {
|
|
status.setText(R.string.incoming_call);
|
|
if (settings.getBoolean("vibrate", false)) {
|
|
long[] pattern = {500, 2000};
|
|
v.vibrate(pattern, 0);
|
|
}
|
|
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);
|
|
}
|
|
|
|
if (settings.getBoolean("show_notification", false)) {
|
|
showNotification();
|
|
}
|
|
|
|
connect.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View view) {
|
|
if (!isPressed && isAppInstalled(LaunchActivity.this, "com.google.android.googlequicksearchbox")) {
|
|
isPressed = true;
|
|
|
|
connect.setImageResource(R.drawable.connect_select);
|
|
|
|
if (!AlarmReceiver.isPlaying()) {
|
|
m = MediaPlayer.create(LaunchActivity.this, R.raw.tone);
|
|
|
|
m.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
|
|
@Override
|
|
public void onPrepared(MediaPlayer mp) {
|
|
mp.start();
|
|
status.setText(R.string.connecting);
|
|
}
|
|
});
|
|
|
|
m.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
|
|
@Override
|
|
public void onCompletion(MediaPlayer mp) {
|
|
mp.release();
|
|
Intent intent = new Intent(LaunchActivity.this, MainActivity.class);
|
|
startActivity(intent);
|
|
}
|
|
});
|
|
} else {
|
|
AlarmReceiver.stopRingtone(LaunchActivity.this);
|
|
notificationManager.cancel(AlarmService.ALARM_NOTIFICATION_ID);
|
|
alarmManager.cancel(pendingIntent);
|
|
v.cancel();
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
cancel.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View view) {
|
|
cancel.setImageResource(R.drawable.cancel_select);
|
|
AlarmReceiver.stopRingtone(getApplicationContext());
|
|
notificationManager.cancel(AlarmService.ALARM_NOTIFICATION_ID);
|
|
alarmManager.cancel(pendingIntent);
|
|
v.cancel();
|
|
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);
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
startActivity(intent);
|
|
}
|
|
});
|
|
|
|
logo.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View view) {
|
|
Intent settingIntent = new Intent(LaunchActivity.this, SettingsActivity.class);
|
|
startActivity(settingIntent);
|
|
}
|
|
});
|
|
}
|
|
|
|
@Override
|
|
protected void attachBaseContext(Context newBase) {
|
|
super.attachBaseContext(LangContext.wrap(newBase));
|
|
}
|
|
|
|
@Override
|
|
protected void onDestroy() {
|
|
super.onDestroy();
|
|
final Window win = getWindow();
|
|
|
|
if (m != null)
|
|
m.release();
|
|
AlarmReceiver.stopRingtone(LaunchActivity.this);
|
|
notificationManager.cancel(AlarmService.ALARM_NOTIFICATION_ID);
|
|
v.cancel();
|
|
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);
|
|
}
|
|
|
|
@Override
|
|
protected void onResume() {
|
|
super.onResume();
|
|
if (isPressed) {
|
|
status.setText(R.string.disconnected);
|
|
} else if (!isAppInstalled(LaunchActivity.this, "com.google.android.googlequicksearchbox")) {
|
|
status.setText(R.string.google_app_error);
|
|
} else if (AlarmReceiver.isPlaying()) {
|
|
status.setText(R.string.incoming_call);
|
|
} else {
|
|
status.setText(R.string.call);
|
|
}
|
|
isPressed = false;
|
|
connect.setImageResource(R.drawable.connect_unselect);
|
|
cancel.setImageResource(R.drawable.cancel_unselect);
|
|
}
|
|
|
|
private void showNotification() {
|
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(LaunchActivity.this)
|
|
.setSmallIcon(R.drawable.xp2)
|
|
.setContentTitle(getString(R.string.app_name))
|
|
.setContentText(getString(R.string.notification_text));
|
|
Intent resultIntent = new Intent(LaunchActivity.this, MainActivity.class);
|
|
TaskStackBuilder stackBuilder = TaskStackBuilder.create(LaunchActivity.this);
|
|
stackBuilder.addParentStack(MainActivity.class);
|
|
stackBuilder.addNextIntent(resultIntent);
|
|
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
builder.setContentIntent(resultPendingIntent);
|
|
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
|
notificationManager.notify(0, builder.build());
|
|
}
|
|
} |