2018-02-22 00:50:11 +01:00
|
|
|
/*
|
2020-08-17 16:17:20 +02:00
|
|
|
* SPDX-FileCopyrightText: 2015 David Edmundson <david@davidedmundson.co.uk>
|
2018-02-22 00:50:11 +01:00
|
|
|
*
|
2020-08-17 16:17:20 +02:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2018-02-22 00:50:11 +01:00
|
|
|
*/
|
|
|
|
|
2015-09-11 16:54:43 +02:00
|
|
|
package org.kde.kdeconnect.Plugins.FindMyPhonePlugin;
|
|
|
|
|
2019-02-22 20:08:21 +01:00
|
|
|
import android.app.Activity;
|
2020-01-05 17:40:54 +00:00
|
|
|
import android.app.NotificationManager;
|
|
|
|
import android.app.PendingIntent;
|
2015-09-11 16:54:43 +02:00
|
|
|
import android.content.Intent;
|
2020-01-05 17:40:54 +00:00
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.media.AudioManager;
|
|
|
|
import android.media.MediaPlayer;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Build;
|
|
|
|
import android.os.PowerManager;
|
|
|
|
import android.preference.PreferenceManager;
|
|
|
|
import android.provider.Settings;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2020-01-10 12:50:23 +01:00
|
|
|
import androidx.annotation.RequiresApi;
|
2020-01-05 17:40:54 +00:00
|
|
|
import androidx.core.app.NotificationCompat;
|
2020-07-07 16:45:02 +05:30
|
|
|
import androidx.core.content.ContextCompat;
|
2015-09-11 16:54:43 +02:00
|
|
|
|
2020-07-12 13:37:24 +05:30
|
|
|
import org.apache.commons.lang3.ArrayUtils;
|
2016-06-28 20:00:19 +02:00
|
|
|
import org.kde.kdeconnect.Helpers.DeviceHelper;
|
2020-01-05 17:40:54 +00:00
|
|
|
import org.kde.kdeconnect.Helpers.NotificationHelper;
|
|
|
|
import org.kde.kdeconnect.MyApplication;
|
2018-03-04 11:31:37 +01:00
|
|
|
import org.kde.kdeconnect.NetworkPacket;
|
2015-09-11 16:54:43 +02:00
|
|
|
import org.kde.kdeconnect.Plugins.Plugin;
|
2019-02-11 20:04:40 +01:00
|
|
|
import org.kde.kdeconnect.Plugins.PluginFactory;
|
2019-01-06 12:26:05 +01:00
|
|
|
import org.kde.kdeconnect.UserInterface.PluginSettingsFragment;
|
2015-09-11 16:54:43 +02:00
|
|
|
import org.kde.kdeconnect_tp.R;
|
|
|
|
|
2020-01-05 17:40:54 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
|
2019-02-11 20:04:40 +01:00
|
|
|
@PluginFactory.LoadablePlugin
|
2015-09-11 16:54:43 +02:00
|
|
|
public class FindMyPhonePlugin extends Plugin {
|
2018-03-04 11:31:37 +01:00
|
|
|
public final static String PACKET_TYPE_FINDMYPHONE_REQUEST = "kdeconnect.findmyphone.request";
|
2016-05-31 17:19:39 +02:00
|
|
|
|
2020-01-05 17:40:54 +00:00
|
|
|
private NotificationManager notificationManager;
|
|
|
|
private int notificationId;
|
|
|
|
private AudioManager audioManager;
|
|
|
|
private MediaPlayer mediaPlayer;
|
2020-01-13 17:31:33 +01:00
|
|
|
private int previousVolume = -1;
|
2020-01-05 17:40:54 +00:00
|
|
|
private PowerManager powerManager;
|
|
|
|
|
2015-09-11 16:54:43 +02:00
|
|
|
@Override
|
|
|
|
public String getDisplayName() {
|
2018-05-10 12:34:28 +02:00
|
|
|
switch (DeviceHelper.getDeviceType(context)) {
|
|
|
|
case Tv:
|
|
|
|
return context.getString(R.string.findmyphone_title_tv);
|
|
|
|
case Tablet:
|
|
|
|
return context.getString(R.string.findmyphone_title_tablet);
|
|
|
|
case Phone:
|
|
|
|
return context.getString(R.string.findmyphone_title);
|
|
|
|
default:
|
|
|
|
return context.getString(R.string.findmyphone_title);
|
|
|
|
}
|
2015-09-11 16:54:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getDescription() {
|
|
|
|
return context.getString(R.string.findmyphone_description);
|
|
|
|
}
|
|
|
|
|
2020-01-05 17:40:54 +00:00
|
|
|
@Override
|
|
|
|
public boolean onCreate() {
|
2020-07-07 16:45:02 +05:30
|
|
|
notificationManager = ContextCompat.getSystemService(context, NotificationManager.class);
|
2020-01-05 17:40:54 +00:00
|
|
|
notificationId = (int) System.currentTimeMillis();
|
2020-07-07 16:45:02 +05:30
|
|
|
audioManager = ContextCompat.getSystemService(context, AudioManager.class);
|
|
|
|
powerManager = ContextCompat.getSystemService(context, PowerManager.class);
|
2020-01-05 17:40:54 +00:00
|
|
|
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
Uri ringtone;
|
|
|
|
String ringtoneString = prefs.getString(context.getString(R.string.findmyphone_preference_key_ringtone), "");
|
|
|
|
if (ringtoneString.isEmpty()) {
|
|
|
|
ringtone = Settings.System.DEFAULT_RINGTONE_URI;
|
|
|
|
} else {
|
|
|
|
ringtone = Uri.parse(ringtoneString);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
mediaPlayer = new MediaPlayer();
|
|
|
|
mediaPlayer.setDataSource(context, ringtone);
|
|
|
|
//TODO: Deprecated use setAudioAttributes for API > 21
|
|
|
|
mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
|
|
|
|
mediaPlayer.setLooping(true);
|
|
|
|
mediaPlayer.prepare();
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e("FindMyPhoneActivity", "Exception", e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
2020-01-13 17:31:33 +01:00
|
|
|
if (mediaPlayer.isPlaying()) {
|
|
|
|
stopPlaying();
|
|
|
|
}
|
2020-01-05 17:40:54 +00:00
|
|
|
audioManager = null;
|
|
|
|
mediaPlayer.release();
|
|
|
|
mediaPlayer = null;
|
|
|
|
}
|
|
|
|
|
2015-09-11 16:54:43 +02:00
|
|
|
@Override
|
2018-03-04 11:31:37 +01:00
|
|
|
public boolean onPacketReceived(NetworkPacket np) {
|
2020-01-05 17:40:54 +00:00
|
|
|
if (Build.VERSION.SDK_INT < 29 || MyApplication.isInForeground()) {
|
|
|
|
Intent intent = new Intent(context, FindMyPhoneActivity.class);
|
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
intent.putExtra(FindMyPhoneActivity.EXTRA_DEVICE_ID, device.getDeviceId());
|
|
|
|
context.startActivity(intent);
|
|
|
|
} else {
|
|
|
|
if (powerManager.isInteractive()) {
|
|
|
|
startPlaying();
|
|
|
|
showBroadcastNotification();
|
|
|
|
} else {
|
|
|
|
showActivityNotification();
|
|
|
|
}
|
|
|
|
}
|
2015-09-11 16:54:43 +02:00
|
|
|
|
2016-06-02 15:35:44 +02:00
|
|
|
return true;
|
2020-01-05 17:40:54 +00:00
|
|
|
}
|
|
|
|
|
2020-01-10 12:50:23 +01:00
|
|
|
@RequiresApi(16)
|
2020-01-05 17:40:54 +00:00
|
|
|
private void showBroadcastNotification() {
|
|
|
|
Intent intent = new Intent(context, FindMyPhoneReceiver.class);
|
|
|
|
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
|
|
|
|
intent.setAction(FindMyPhoneReceiver.ACTION_FOUND_IT);
|
|
|
|
intent.putExtra(FindMyPhoneReceiver.EXTRA_DEVICE_ID, device.getDeviceId());
|
|
|
|
|
|
|
|
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
|
|
|
|
createNotification(pendingIntent);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void showActivityNotification() {
|
|
|
|
Intent intent = new Intent(context, FindMyPhoneActivity.class);
|
|
|
|
intent.putExtra(FindMyPhoneActivity.EXTRA_DEVICE_ID, device.getDeviceId());
|
|
|
|
|
|
|
|
PendingIntent pi = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
createNotification(pi);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void createNotification(PendingIntent pendingIntent) {
|
|
|
|
NotificationCompat.Builder notification = new NotificationCompat.Builder(context, NotificationHelper.Channels.HIGHPRIORITY);
|
|
|
|
notification
|
|
|
|
.setSmallIcon(R.drawable.ic_notification)
|
|
|
|
.setOngoing(false)
|
|
|
|
.setFullScreenIntent(pendingIntent, true)
|
|
|
|
.setPriority(NotificationCompat.PRIORITY_HIGH)
|
|
|
|
.setAutoCancel(true)
|
|
|
|
.setContentTitle(context.getString(R.string.findmyphone_found));
|
|
|
|
notification.setGroup("BackgroundService");
|
|
|
|
|
|
|
|
notificationManager.notify(notificationId, notification.build());
|
|
|
|
}
|
|
|
|
|
|
|
|
void startPlaying() {
|
2020-01-10 12:50:14 +01:00
|
|
|
if (mediaPlayer != null && !mediaPlayer.isPlaying()) {
|
2020-01-05 17:40:54 +00:00
|
|
|
// Make sure we are heard even when the phone is silent, restore original volume later
|
|
|
|
previousVolume = audioManager.getStreamVolume(AudioManager.STREAM_ALARM);
|
|
|
|
audioManager.setStreamVolume(AudioManager.STREAM_ALARM, audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM), 0);
|
|
|
|
|
|
|
|
mediaPlayer.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void hideNotification() {
|
|
|
|
notificationManager.cancel(notificationId);
|
|
|
|
}
|
|
|
|
|
|
|
|
void stopPlaying() {
|
2020-04-17 22:03:37 +02:00
|
|
|
if (audioManager == null) {
|
|
|
|
// The Plugin was destroyed (probably the device disconnected)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-13 17:31:33 +01:00
|
|
|
if (previousVolume != -1) {
|
|
|
|
audioManager.setStreamVolume(AudioManager.STREAM_ALARM, previousVolume, 0);
|
|
|
|
}
|
2020-01-05 17:40:54 +00:00
|
|
|
mediaPlayer.stop();
|
|
|
|
try {
|
|
|
|
mediaPlayer.prepare();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2015-09-11 16:54:43 +02:00
|
|
|
|
2020-01-05 17:40:54 +00:00
|
|
|
boolean isPlaying() {
|
|
|
|
return mediaPlayer.isPlaying();
|
2015-09-11 16:54:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-03-04 11:31:37 +01:00
|
|
|
public String[] getSupportedPacketTypes() {
|
|
|
|
return new String[]{PACKET_TYPE_FINDMYPHONE_REQUEST};
|
2015-09-11 16:54:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-03-04 11:31:37 +01:00
|
|
|
public String[] getOutgoingPacketTypes() {
|
2020-07-12 13:37:24 +05:30
|
|
|
return ArrayUtils.EMPTY_STRING_ARRAY;
|
2015-09-11 16:54:43 +02:00
|
|
|
}
|
2018-02-22 00:50:11 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasSettings() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-06 12:26:05 +01:00
|
|
|
@Override
|
2019-02-22 20:08:21 +01:00
|
|
|
public PluginSettingsFragment getSettingsFragment(Activity activity) {
|
2019-01-06 12:26:05 +01:00
|
|
|
return FindMyPhoneSettingsFragment.newInstance(getPluginKey());
|
|
|
|
}
|
2015-09-11 16:54:43 +02:00
|
|
|
}
|