2019-02-13 20:43:30 +01:00
|
|
|
/* Copyright (C) 2018-2019 Andreas Shimokawa, Carsten Pfeiffer, Daniele
|
|
|
|
Gobbetti
|
2018-01-13 18:46:21 +01:00
|
|
|
|
|
|
|
This file is part of Gadgetbridge.
|
|
|
|
|
|
|
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Gadgetbridge is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
package nodomain.freeyourgadget.gadgetbridge.activities;
|
|
|
|
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.IntentFilter;
|
|
|
|
import android.media.AudioManager;
|
|
|
|
import android.media.MediaPlayer;
|
|
|
|
import android.media.RingtoneManager;
|
|
|
|
import android.net.Uri;
|
2019-10-10 11:32:52 +02:00
|
|
|
import android.os.Build;
|
2018-01-13 18:46:21 +01:00
|
|
|
import android.os.Bundle;
|
2019-10-10 11:32:52 +02:00
|
|
|
import android.os.VibrationEffect;
|
|
|
|
import android.os.Vibrator;
|
2018-01-13 18:46:21 +01:00
|
|
|
import android.view.View;
|
|
|
|
import android.widget.Button;
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
2019-01-26 15:52:40 +01:00
|
|
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
2018-01-13 18:46:21 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
|
|
|
|
|
|
|
|
|
|
|
public class FindPhoneActivity extends AbstractGBActivity {
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(FindPhoneActivity.class);
|
|
|
|
|
|
|
|
public static final String ACTION_FOUND
|
|
|
|
= "nodomain.freeyourgadget.gadgetbridge.findphone.action.reply";
|
|
|
|
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
String action = intent.getAction();
|
|
|
|
if (action != null) {
|
|
|
|
switch (action) {
|
|
|
|
case ACTION_FOUND: {
|
|
|
|
finish();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-10 11:32:52 +02:00
|
|
|
Vibrator mVibrator;
|
2018-01-13 18:46:21 +01:00
|
|
|
AudioManager mAudioManager;
|
|
|
|
int userVolume;
|
|
|
|
MediaPlayer mp;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_find_phone);
|
|
|
|
|
|
|
|
IntentFilter filter = new IntentFilter();
|
|
|
|
filter.addAction(ACTION_FOUND);
|
|
|
|
LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filter);
|
|
|
|
registerReceiver(mReceiver, filter); // for ACTION_FOUND
|
|
|
|
|
|
|
|
Button foundButton = (Button) findViewById(R.id.foundbutton);
|
|
|
|
foundButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
});
|
2019-10-10 11:32:52 +02:00
|
|
|
|
|
|
|
vibrate();
|
2018-01-13 18:46:21 +01:00
|
|
|
playRingtone();
|
|
|
|
}
|
|
|
|
|
2019-10-10 11:32:52 +02:00
|
|
|
private void vibrate(){
|
|
|
|
mVibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
|
|
|
|
|
|
|
|
long[] vibrationPattern = new long[]{ 1000, 1000 };
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
VibrationEffect vibrationEffect = VibrationEffect.createWaveform(vibrationPattern, 0);
|
|
|
|
|
|
|
|
mVibrator.vibrate(vibrationEffect);
|
|
|
|
} else {
|
|
|
|
mVibrator.vibrate(vibrationPattern, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void playRingtone(){
|
2018-01-13 18:46:21 +01:00
|
|
|
mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
|
|
|
|
if (mAudioManager != null) {
|
|
|
|
userVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_ALARM);
|
|
|
|
}
|
|
|
|
mp = new MediaPlayer();
|
|
|
|
|
|
|
|
Uri ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
|
|
|
|
|
|
|
|
try {
|
|
|
|
mp.setDataSource(this, ringtoneUri);
|
|
|
|
mp.setAudioStreamType(AudioManager.STREAM_ALARM);
|
|
|
|
mp.setLooping(true);
|
|
|
|
mp.prepare();
|
|
|
|
mp.start();
|
|
|
|
} catch (IOException ignore) {
|
2018-12-18 10:26:29 +01:00
|
|
|
LOG.warn("problem playing ringtone");
|
2018-01-13 18:46:21 +01:00
|
|
|
}
|
|
|
|
|
2018-12-18 10:26:29 +01:00
|
|
|
if (mAudioManager != null) {
|
|
|
|
userVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_ALARM);
|
|
|
|
mAudioManager.setStreamVolume(AudioManager.STREAM_ALARM, mAudioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM), AudioManager.FLAG_PLAY_SOUND);
|
|
|
|
}
|
2018-01-13 18:46:21 +01:00
|
|
|
}
|
|
|
|
|
2019-10-10 11:32:52 +02:00
|
|
|
private void stopVibration() {
|
|
|
|
mVibrator.cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void stopSound() {
|
2018-01-13 18:46:21 +01:00
|
|
|
mAudioManager.setStreamVolume(AudioManager.STREAM_ALARM, userVolume, AudioManager.FLAG_PLAY_SOUND);
|
|
|
|
mp.stop();
|
|
|
|
mp.reset();
|
|
|
|
mp.release();
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
protected void onDestroy() {
|
|
|
|
super.onDestroy();
|
2019-10-10 11:32:52 +02:00
|
|
|
|
|
|
|
stopVibration();
|
2018-01-13 18:46:21 +01:00
|
|
|
stopSound();
|
2019-10-10 11:32:52 +02:00
|
|
|
|
2018-01-13 18:46:21 +01:00
|
|
|
LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
|
|
|
|
unregisterReceiver(mReceiver);
|
|
|
|
}
|
|
|
|
}
|