use of org.kde.kdeconnect.Plugins.Plugin in project kdeconnect-android by KDE.
the class Device method removePlugin.
private synchronized boolean removePlugin(String pluginKey) {
Plugin plugin = plugins.remove(pluginKey);
Plugin failedPlugin = failedPlugins.remove(pluginKey);
if (plugin == null) {
if (failedPlugin == null) {
//Not found
return false;
}
plugin = failedPlugin;
}
try {
plugin.onDestroy();
//Log.e("removePlugin","removed " + pluginKey);
} catch (Exception e) {
e.printStackTrace();
Log.e("KDE/removePlugin", "Exception calling onDestroy for plugin " + pluginKey);
}
return true;
}
use of org.kde.kdeconnect.Plugins.Plugin in project kdeconnect-android by KDE.
the class DeviceFragment method refreshUI.
void refreshUI() {
if (device == null || rootView == null) {
return;
}
//Once in-app, there is no point in keep displaying the notification if any
device.hidePairingNotification();
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
if (device.isPairRequestedByPeer()) {
((TextView) rootView.findViewById(R.id.pair_message)).setText(R.string.pair_requested);
rootView.findViewById(R.id.pair_progress).setVisibility(View.GONE);
rootView.findViewById(R.id.pair_button).setVisibility(View.GONE);
rootView.findViewById(R.id.pair_request).setVisibility(View.VISIBLE);
} else {
boolean paired = device.isPaired();
boolean reachable = device.isReachable();
boolean onData = NetworkHelper.isOnMobileNetwork(getContext());
rootView.findViewById(R.id.pairing_buttons).setVisibility(paired ? View.GONE : View.VISIBLE);
rootView.findViewById(R.id.not_reachable_message).setVisibility((paired && !reachable && !onData) ? View.VISIBLE : View.GONE);
rootView.findViewById(R.id.on_data_message).setVisibility((paired && !reachable && onData) ? View.VISIBLE : View.GONE);
try {
ArrayList<ListAdapter.Item> items = new ArrayList<>();
//Plugins button list
final Collection<Plugin> plugins = device.getLoadedPlugins().values();
for (final Plugin p : plugins) {
if (!p.hasMainActivity())
continue;
if (p.displayInContextMenu())
continue;
items.add(new PluginItem(p, new View.OnClickListener() {
@Override
public void onClick(View v) {
p.startMainActivity(mActivity);
}
}));
}
//Failed plugins List
final ConcurrentHashMap<String, Plugin> failed = device.getFailedPlugins();
if (!failed.isEmpty()) {
if (errorHeader == null) {
errorHeader = new TextView(mActivity);
errorHeader.setPadding(0, ((int) (28 * getResources().getDisplayMetrics().density)), 0, ((int) (8 * getResources().getDisplayMetrics().density)));
errorHeader.setOnClickListener(null);
errorHeader.setOnLongClickListener(null);
errorHeader.setText(getResources().getString(R.string.plugins_failed_to_load));
}
items.add(new CustomItem(errorHeader));
for (Map.Entry<String, Plugin> entry : failed.entrySet()) {
String pluginKey = entry.getKey();
final Plugin plugin = entry.getValue();
if (plugin == null) {
items.add(new SmallEntryItem(pluginKey));
} else {
items.add(new SmallEntryItem(plugin.getDisplayName(), new View.OnClickListener() {
@Override
public void onClick(View v) {
plugin.getErrorDialog(mActivity).show();
}
}));
}
}
}
//Plugins without permissions List
final ConcurrentHashMap<String, Plugin> permissionsNeeded = device.getPluginsWithoutPermissions();
if (!permissionsNeeded.isEmpty()) {
if (noPermissionsHeader == null) {
noPermissionsHeader = new TextView(mActivity);
noPermissionsHeader.setPadding(0, ((int) (28 * getResources().getDisplayMetrics().density)), 0, ((int) (8 * getResources().getDisplayMetrics().density)));
noPermissionsHeader.setOnClickListener(null);
noPermissionsHeader.setOnLongClickListener(null);
noPermissionsHeader.setText(getResources().getString(R.string.plugins_need_permission));
}
items.add(new CustomItem(noPermissionsHeader));
for (Map.Entry<String, Plugin> entry : permissionsNeeded.entrySet()) {
String pluginKey = entry.getKey();
final Plugin plugin = entry.getValue();
if (plugin == null) {
items.add(new SmallEntryItem(pluginKey));
} else {
items.add(new SmallEntryItem(plugin.getDisplayName(), new View.OnClickListener() {
@Override
public void onClick(View v) {
plugin.getPermissionExplanationDialog(mActivity).show();
}
}));
}
}
}
ListView buttonsList = (ListView) rootView.findViewById(R.id.buttons_list);
ListAdapter adapter = new ListAdapter(mActivity, items);
buttonsList.setAdapter(adapter);
mActivity.invalidateOptionsMenu();
} catch (IllegalStateException e) {
e.printStackTrace();
//Ignore: The activity was closed while we were trying to update it
} catch (ConcurrentModificationException e) {
Log.e("DeviceActivity", "ConcurrentModificationException");
//Try again
this.run();
}
}
}
});
}
use of org.kde.kdeconnect.Plugins.Plugin in project kdeconnect-android by KDE.
the class Device method addPlugin.
private synchronized boolean addPlugin(final String pluginKey) {
Plugin existing = plugins.get(pluginKey);
if (existing != null) {
//Log.w("KDE/addPlugin","plugin already present:" + pluginKey);
return true;
}
final Plugin plugin = PluginFactory.instantiatePluginForDevice(context, pluginKey, this);
if (plugin == null) {
Log.e("KDE/addPlugin", "could not instantiate plugin: " + pluginKey);
//failedPlugins.put(pluginKey, null);
return false;
}
boolean success;
try {
success = plugin.onCreate();
} catch (Exception e) {
success = false;
e.printStackTrace();
Log.e("KDE/addPlugin", "Exception loading plugin " + pluginKey);
}
if (success) {
//Log.e("addPlugin","added " + pluginKey);
failedPlugins.remove(pluginKey);
plugins.put(pluginKey, plugin);
} else {
Log.e("KDE/addPlugin", "plugin failed to load " + pluginKey);
plugins.remove(pluginKey);
failedPlugins.put(pluginKey, plugin);
}
if (!plugin.checkRequiredPermissions()) {
Log.e("KDE/addPlugin", "No permission " + pluginKey);
plugins.remove(pluginKey);
pluginsWithoutPermissions.put(pluginKey, plugin);
success = false;
} else {
Log.i("KDE/addPlugin", "Permission OK " + pluginKey);
pluginsWithoutPermissions.remove(pluginKey);
}
return success;
}
use of org.kde.kdeconnect.Plugins.Plugin in project kdeconnect-android by KDE.
the class Device method onPackageReceived.
@Override
public void onPackageReceived(NetworkPackage np) {
hackToMakeRetrocompatiblePacketTypes(np);
if (NetworkPackage.PACKAGE_TYPE_PAIR.equals(np.getType())) {
Log.i("KDE/Device", "Pair package");
for (BasePairingHandler ph : pairingHandlers.values()) {
try {
ph.packageReceived(np);
} catch (Exception e) {
e.printStackTrace();
Log.e("PairingPackageReceived", "Exception");
}
}
} else if (isPaired()) {
//If capabilities are not supported, iterate all plugins
Collection<String> targetPlugins = pluginsByIncomingInterface.get(np.getType());
if (targetPlugins != null && !targetPlugins.isEmpty()) {
for (String pluginKey : targetPlugins) {
Plugin plugin = plugins.get(pluginKey);
try {
plugin.onPackageReceived(np);
} catch (Exception e) {
e.printStackTrace();
Log.e("KDE/Device", "Exception in " + plugin.getPluginKey() + "'s onPackageReceived()");
//try { Log.e("KDE/Device", "NetworkPackage:" + np.serialize()); } catch (Exception _) { }
}
}
} else {
Log.w("Device", "Ignoring packet with type " + np.getType() + " because no plugin can handle it");
}
} else {
//Log.e("KDE/onPackageReceived","Device not paired, will pass package to unpairedPackageListeners");
// If it is pair package, it should be captured by "if" at start
// If not and device is paired, it should be captured by isPaired
// Else unpair, this handles the situation when one device unpairs, but other dont know like unpairing when wi-fi is off
unpair();
//If capabilities are not supported, iterate all plugins
Collection<String> targetPlugins = pluginsByIncomingInterface.get(np.getType());
if (targetPlugins != null && !targetPlugins.isEmpty()) {
for (String pluginKey : targetPlugins) {
Plugin plugin = plugins.get(pluginKey);
try {
plugin.onUnpairedDevicePackageReceived(np);
} catch (Exception e) {
e.printStackTrace();
Log.e("KDE/Device", "Exception in " + plugin.getDisplayName() + "'s onPackageReceived() in unPairedPackageListeners");
}
}
} else {
Log.e("Device", "Ignoring packet with type " + np.getType() + " because no plugin can handle it");
}
}
}
use of org.kde.kdeconnect.Plugins.Plugin in project kdeconnect-android by KDE.
the class DeviceFragment method onPrepareOptionsMenu.
@Override
public void onPrepareOptionsMenu(Menu menu) {
//Log.e("DeviceFragment", "onPrepareOptionsMenu");
super.onPrepareOptionsMenu(menu);
menu.clear();
if (device == null) {
return;
}
//Plugins button list
final Collection<Plugin> plugins = device.getLoadedPlugins().values();
for (final Plugin p : plugins) {
if (!p.displayInContextMenu()) {
continue;
}
menu.add(p.getActionName()).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
p.startMainActivity(mActivity);
return true;
}
});
}
menu.add(R.string.device_menu_plugins).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
Intent intent = new Intent(mActivity, SettingsActivity.class);
intent.putExtra("deviceId", mDeviceId);
startActivity(intent);
return true;
}
});
if (device.isPaired() && device.isReachable()) {
menu.add(R.string.encryption_info_title).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
Context context = mActivity;
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(context.getResources().getString(R.string.encryption_info_title));
builder.setPositiveButton(context.getResources().getString(R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
if (device.certificate == null) {
builder.setMessage(R.string.encryption_info_msg_no_ssl);
} else {
builder.setMessage(context.getResources().getString(R.string.my_device_fingerprint) + "\n" + SslHelper.getCertificateHash(SslHelper.certificate) + "\n\n" + context.getResources().getString(R.string.remote_device_fingerprint) + "\n" + SslHelper.getCertificateHash(device.certificate));
}
builder.create().show();
return true;
}
});
}
if (device.isPaired()) {
menu.add(R.string.device_menu_unpair).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
//Remove listener so buttons don't show for a while before changing the view
device.removePluginsChangedListener(pluginsChangedListener);
device.removePairingCallback(pairingCallback);
device.unpair();
mActivity.onDeviceSelected(null);
return true;
}
});
}
}
Aggregations