When things get complicated... REFACTOR!

This commit is contained in:
RIP95 2017-03-20 23:06:55 +03:00
parent 4800400b7c
commit c5c517a69b
7 changed files with 72 additions and 75 deletions

View File

@ -19,19 +19,14 @@ import java.util.Calendar;
public class AlarmActivity extends Activity {
final String TAG = "Amadeus.Alarm";
AlarmManager alarmManager;
PendingIntent pendingIntent;
TimePicker alarmTimePicker;
ToggleButton alarmToggle;
public static final int alarmCode = 104856;
SharedPreferences settings;
SharedPreferences.Editor editor;
private final String TAG = "Amadeus.Alarm";
public static final int ALARM_ID = 104859;
@Override
public void onStart() {
super.onStart();
}
private AlarmManager alarmManager;
private PendingIntent pendingIntent;
private TimePicker alarmTimePicker;
private ToggleButton alarmToggle;
private SharedPreferences settings;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -42,7 +37,7 @@ public class AlarmActivity extends Activity {
alarmToggle = (ToggleButton) findViewById(R.id.alarmToggle);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, alarmCode, alarmIntent, PendingIntent.FLAG_NO_CREATE);
pendingIntent = PendingIntent.getBroadcast(this, ALARM_ID, alarmIntent, PendingIntent.FLAG_NO_CREATE);
if (settings.getBoolean("alarm_toggle", false)) {
alarmToggle.setChecked(true);
@ -52,7 +47,7 @@ public class AlarmActivity extends Activity {
}
public void onToggleClicked(View view) {
editor = settings.edit();
SharedPreferences.Editor editor = settings.edit();
if (alarmToggle.isChecked()) {
editor.putBoolean("alarm_toggle", true);
Calendar calendar = Calendar.getInstance();

View File

@ -11,10 +11,9 @@ import android.support.v4.content.WakefulBroadcastReceiver;
public class AlarmReceiver extends WakefulBroadcastReceiver {
static MediaPlayer m;
static boolean isPlaying = false;
static SharedPreferences settings;
static SharedPreferences.Editor editor;
private static MediaPlayer m;
private static boolean isPlaying = false;
private static SharedPreferences settings;
@Override
public void onReceive(Context context, Intent intent) {
@ -46,7 +45,7 @@ public class AlarmReceiver extends WakefulBroadcastReceiver {
public static void stopRingtone(Context context) {
settings = PreferenceManager.getDefaultSharedPreferences(context);
if (isPlaying) {
editor = settings.edit();
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("alarm_toggle", false);
editor.apply();
m.release();

View File

@ -8,7 +8,8 @@ import android.content.Intent;
import android.support.v4.app.NotificationCompat;
public class AlarmService extends IntentService {
NotificationManager alarmNotificationManager;
public static final int ALARM_NOTIFICATION_ID = 102434;
public AlarmService() {
super("AlarmService");
@ -23,7 +24,7 @@ public class AlarmService extends IntentService {
}
private void sendNotification(String msg) {
alarmNotificationManager = (NotificationManager) this
NotificationManager alarmNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
@ -35,6 +36,6 @@ public class AlarmService extends IntentService {
.setContentText(msg);
alarmNotificationBuilder.setContentIntent(contentIntent);
alarmNotificationManager.notify(1, alarmNotificationBuilder.build());
alarmNotificationManager.notify(ALARM_NOTIFICATION_ID, alarmNotificationBuilder.build());
}
}

View File

@ -19,12 +19,11 @@ public class LangContext extends ContextWrapper {
@SuppressWarnings("deprecation")
public static ContextWrapper wrap(Context context) {
SharedPreferences sharedPreferences;
Configuration config = context.getResources().getConfiguration();
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
String lang = sharedPreferences.getString("lang", "ja");
String lang = settings.getString("lang", "en");
Locale locale = new Locale(lang);
Locale.setDefault(locale);

View File

@ -23,28 +23,27 @@ import android.widget.ImageView;
import android.widget.TextView;
public class LaunchActivity extends AppCompatActivity {
ImageView connect, cancel, imageViewLogo;
TextView status;
Boolean isPressed = false;
SharedPreferences settings;
MediaPlayer m;
Handler aniHandle = new Handler();
AlarmManager alarmManager;
PendingIntent pendingIntent;
NotificationManager notificationManager;
Vibrator v;
int i = 0;
int id;
int duration = 20;
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);
id = getResources().getIdentifier(imgName, "drawable", getPackageName());
imageViewLogo.setImageDrawable((ContextCompat.getDrawable(LaunchActivity.this, id)));
aniHandle.postDelayed(this, duration);
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());
logo.setImageDrawable((ContextCompat.getDrawable(LaunchActivity.this, id)));
aniHandle.postDelayed(this, DURATION);
}
}
};
@ -65,18 +64,21 @@ public class LaunchActivity extends AppCompatActivity {
connect = (ImageView) findViewById(R.id.imageView_connect);
cancel = (ImageView) findViewById(R.id.imageView_cancel);
status = (TextView) findViewById(R.id.textView_call);
imageViewLogo = (ImageView) findViewById(R.id.imageView_logo);
aniHandle.post(aniRunnable);
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);
pendingIntent = PendingIntent.getBroadcast(LaunchActivity.this, AlarmActivity.alarmCode, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
final PendingIntent pendingIntent = PendingIntent.getBroadcast(LaunchActivity.this, AlarmActivity.ALARM_ID, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationManager = (NotificationManager) getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
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)) {
@ -87,9 +89,6 @@ public class LaunchActivity extends AppCompatActivity {
win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}
connect.setImageResource(R.drawable.connect_unselect);
cancel.setImageResource(R.drawable.cancel_unselect);
if (settings.getBoolean("show_notification", false)) {
showNotification();
}
@ -123,7 +122,7 @@ public class LaunchActivity extends AppCompatActivity {
});
} else {
AlarmReceiver.stopRingtone(LaunchActivity.this);
notificationManager.cancel(1);
notificationManager.cancel(AlarmService.ALARM_NOTIFICATION_ID);
alarmManager.cancel(pendingIntent);
v.cancel();
win.clearFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
@ -141,7 +140,7 @@ public class LaunchActivity extends AppCompatActivity {
public void onClick(View view) {
cancel.setImageResource(R.drawable.cancel_select);
AlarmReceiver.stopRingtone(getApplicationContext());
notificationManager.cancel(1);
notificationManager.cancel(AlarmService.ALARM_NOTIFICATION_ID);
alarmManager.cancel(pendingIntent);
v.cancel();
win.clearFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
@ -154,7 +153,7 @@ public class LaunchActivity extends AppCompatActivity {
}
});
imageViewLogo.setOnClickListener(new View.OnClickListener() {
logo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent settingIntent = new Intent(LaunchActivity.this, SettingsActivity.class);
@ -176,7 +175,7 @@ public class LaunchActivity extends AppCompatActivity {
if (m != null)
m.release();
AlarmReceiver.stopRingtone(LaunchActivity.this);
notificationManager.cancel(1);
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);
@ -185,6 +184,7 @@ public class LaunchActivity extends AppCompatActivity {
@Override
protected void onResume() {
super.onResume();
if (isPressed) {
status.setText(R.string.disconnected);
} else if (!isAppInstalled(LaunchActivity.this, "com.google.android.googlequicksearchbox")) {
@ -197,7 +197,6 @@ public class LaunchActivity extends AppCompatActivity {
isPressed = false;
connect.setImageResource(R.drawable.connect_unselect);
cancel.setImageResource(R.drawable.cancel_unselect);
super.onResume();
}
private void showNotification() {
@ -211,7 +210,7 @@ public class LaunchActivity extends AppCompatActivity {
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
}
}

View File

@ -38,20 +38,21 @@ import java.util.List;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
final String TAG = "Amadeus";
final int REQUEST_PERMISSION_RECORD_AUDIO = 1;
TextView subtitles;
ImageView kurisu;
Boolean isLoop = false;
Boolean isSpeaking = false;
VoiceLine[] voiceLines = VoiceLine.Line.getLines();
AnimationDrawable animation;
int shaman_girls = -1;
Random randomgen = new Random();
SharedPreferences settings;
String lang, recogLang;
MediaPlayer m;
String[] contextLang;
private final String TAG = "Amadeus";
private TextView subtitles;
private ImageView kurisu;
private Boolean isLoop = false;
private Boolean isSpeaking = false;
private VoiceLine[] voiceLines = VoiceLine.Line.getLines();
private AnimationDrawable animation;
private int shaman_girls = -1;
private Random randomgen = new Random();
private SharedPreferences settings;
private String recogLang;
private MediaPlayer m;
private String[] contextLang;
private SpeechRecognizer sr;
@Override
@ -62,21 +63,22 @@ public class MainActivity extends AppCompatActivity {
subtitles = (TextView) findViewById(R.id.textView_subtitles);
ImageView subtitlesBackground = (ImageView) findViewById(R.id.imageView_subtitles);
settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
lang = settings.getString("lang", "ja");
recogLang = settings.getString("recognition_lang", "ja-JP");
contextLang = recogLang.split("-");
sr = SpeechRecognizer.createSpeechRecognizer(this);
sr.setRecognitionListener(new listener());
final Handler handler = new Handler();
final int REQUEST_PERMISSION_RECORD_AUDIO = 11302;
if (!settings.getBoolean("show_subtitles", false)) {
subtitlesBackground.setVisibility(View.INVISIBLE);
}
speak(voiceLines[VoiceLine.Line.HELLO]);
final Handler handler = new Handler();
speak(voiceLines[VoiceLine.Line.HELLO]);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.RECORD_AUDIO}, REQUEST_PERMISSION_RECORD_AUDIO);
}
sr = SpeechRecognizer.createSpeechRecognizer(this);
sr.setRecognitionListener(new listener());
final Runnable loop = new Runnable() {
@Override
@ -87,6 +89,7 @@ public class MainActivity extends AppCompatActivity {
}
}
};
kurisu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
@ -456,7 +459,8 @@ public class MainActivity extends AppCompatActivity {
}
private class listener implements RecognitionListener {
final String TAG = "Amadeus.listener";
private final String TAG = "Amadeus.listener";
public void onReadyForSpeech(Bundle params) {
Log.d(TAG, "Speech recognition start");

View File

@ -4,7 +4,7 @@ package com.example.yink.amadeus;
* Created by Yink on 28.02.2017.
*/
class VoiceLine {
public class VoiceLine {
final private int id;
final private int mood;
final private int subtitle;