File indexing completed on 2024-12-22 04:41:42
0001 /* 0002 * SPDX-FileCopyrightText: 2014 Albert Vaca Cintora <albertvaka@gmail.com> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 package org.kde.kdeconnect.UserInterface; 0008 0009 import android.os.Build; 0010 import android.os.Bundle; 0011 import android.view.Menu; 0012 import android.view.MenuItem; 0013 import android.view.View; 0014 import android.widget.TextView; 0015 0016 import androidx.appcompat.app.AlertDialog; 0017 import androidx.appcompat.app.AppCompatActivity; 0018 import androidx.fragment.app.Fragment; 0019 import androidx.fragment.app.FragmentManager; 0020 0021 import com.google.android.material.dialog.MaterialAlertDialogBuilder; 0022 0023 import org.kde.kdeconnect.Device; 0024 import org.kde.kdeconnect.DeviceStats; 0025 import org.kde.kdeconnect.KdeConnect; 0026 import org.kde.kdeconnect.Plugins.Plugin; 0027 import org.kde.kdeconnect_tp.R; 0028 0029 import java.util.Objects; 0030 0031 public class PluginSettingsActivity 0032 extends AppCompatActivity 0033 implements PluginPreference.PluginPreferenceCallback { 0034 0035 public static final String EXTRA_DEVICE_ID = "deviceId"; 0036 public static final String EXTRA_PLUGIN_KEY = "pluginKey"; 0037 0038 //TODO: Save/restore state 0039 static private String deviceId; //Static because if we get here by using the back button in the action bar, the extra deviceId will not be set. 0040 0041 @Override 0042 public void onCreate(Bundle savedInstanceState) { 0043 super.onCreate(savedInstanceState); 0044 0045 setContentView(R.layout.activity_plugin_settings); 0046 0047 setSupportActionBar(findViewById(R.id.toolbar)); 0048 Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true); 0049 getSupportActionBar().setDisplayShowHomeEnabled(true); 0050 0051 String pluginKey = null; 0052 0053 if (getIntent().hasExtra(EXTRA_DEVICE_ID)) { 0054 deviceId = getIntent().getStringExtra(EXTRA_DEVICE_ID); 0055 0056 if (getIntent().hasExtra(EXTRA_PLUGIN_KEY)) { 0057 pluginKey = getIntent().getStringExtra(EXTRA_PLUGIN_KEY); 0058 } 0059 } else if (deviceId == null) { 0060 throw new RuntimeException("You must start DeviceSettingActivity using an intent that has a " + EXTRA_DEVICE_ID + " extra"); 0061 } 0062 0063 Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragmentPlaceHolder); 0064 if (fragment == null) { 0065 if (pluginKey != null) { 0066 Device device = KdeConnect.getInstance().getDevice(deviceId); 0067 if (device != null) { 0068 Plugin plugin = device.getPluginIncludingWithoutPermissions(pluginKey); 0069 if (plugin != null) { 0070 fragment = plugin.getSettingsFragment(this); 0071 } 0072 } 0073 } 0074 if (fragment == null) { 0075 fragment = PluginSettingsListFragment.newInstance(deviceId); 0076 } 0077 0078 getSupportFragmentManager() 0079 .beginTransaction() 0080 .add(R.id.fragmentPlaceHolder, fragment) 0081 .commit(); 0082 } 0083 } 0084 0085 @Override 0086 public boolean onOptionsItemSelected(MenuItem item) { 0087 if (item.getItemId() == android.R.id.home) { 0088 FragmentManager fm = getSupportFragmentManager(); 0089 0090 if (fm.getBackStackEntryCount() > 0) { 0091 fm.popBackStack(); 0092 return true; 0093 } 0094 } 0095 0096 return super.onOptionsItemSelected(item); 0097 } 0098 0099 @Override 0100 public boolean onPrepareOptionsMenu(Menu menu) { 0101 super.onPrepareOptionsMenu(menu); 0102 menu.clear(); 0103 if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.N) { 0104 return false; // PacketStats not working in API < 24 0105 } 0106 menu.add(R.string.plugin_stats).setOnMenuItemClickListener(item -> { 0107 String stats = DeviceStats.getStatsForDevice(deviceId); 0108 AlertDialog alertDialog = new MaterialAlertDialogBuilder(PluginSettingsActivity.this) 0109 .setTitle(R.string.plugin_stats) 0110 .setPositiveButton(R.string.ok, (dialog, which) -> dialog.dismiss()) 0111 .setMessage(stats) 0112 .show(); 0113 View messageView = alertDialog.findViewById(android.R.id.message); 0114 if (messageView instanceof TextView) { 0115 ((TextView) messageView).setTextIsSelectable(true); 0116 } 0117 return true; 0118 }); 0119 return true; 0120 } 0121 0122 @Override 0123 public void onStartPluginSettingsFragment(Plugin plugin) { 0124 setTitle(getString(R.string.plugin_settings_with_name, plugin.getDisplayName())); 0125 0126 PluginSettingsFragment fragment = plugin.getSettingsFragment(this); 0127 0128 //TODO: Remove when NotificationFilterActivity has been turned into a PluginSettingsFragment 0129 if (fragment == null) { 0130 return; 0131 } 0132 0133 getSupportFragmentManager() 0134 .beginTransaction() 0135 .setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_left, R.anim.slide_out_right) 0136 .replace(R.id.fragmentPlaceHolder, fragment) 0137 .addToBackStack(null) 0138 .commit(); 0139 0140 } 0141 0142 @Override 0143 public boolean onSupportNavigateUp() { 0144 super.onBackPressed(); 0145 return true; 0146 } 0147 0148 @Override 0149 public void onFinish() { 0150 finish(); 0151 } 0152 0153 public String getSettingsDeviceId() { // Weird name because Activity also has a getDeviceId() 0154 return deviceId; 0155 } 0156 }