File indexing completed on 2024-12-22 04:41:41

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.Plugins.SharePlugin;
0008 
0009 import android.content.Intent;
0010 import android.os.Bundle;
0011 import android.view.Menu;
0012 import android.view.MenuInflater;
0013 import android.view.MenuItem;
0014 
0015 import androidx.appcompat.app.ActionBar;
0016 import androidx.appcompat.app.AppCompatActivity;
0017 
0018 import org.kde.kdeconnect.BackgroundService;
0019 import org.kde.kdeconnect.Device;
0020 import org.kde.kdeconnect.KdeConnect;
0021 import org.kde.kdeconnect.UserInterface.List.EntryItemWithIcon;
0022 import org.kde.kdeconnect.UserInterface.List.ListAdapter;
0023 import org.kde.kdeconnect.UserInterface.List.SectionItem;
0024 import org.kde.kdeconnect_tp.R;
0025 import org.kde.kdeconnect_tp.databinding.ActivityShareBinding;
0026 
0027 import java.util.ArrayList;
0028 import java.util.Collection;
0029 import java.util.Objects;
0030 
0031 
0032 public class ShareActivity extends AppCompatActivity {
0033     private ActivityShareBinding binding;
0034 
0035     @Override
0036     public boolean onCreateOptionsMenu(Menu menu) {
0037         MenuInflater inflater = getMenuInflater();
0038         inflater.inflate(R.menu.refresh, menu);
0039         return true;
0040     }
0041 
0042     @Override
0043     public boolean onOptionsItemSelected(final MenuItem item) {
0044         if (item.getItemId() == R.id.menu_refresh) {
0045             refreshDevicesAction();
0046             return true;
0047         } else {
0048             return super.onOptionsItemSelected(item);
0049         }
0050     }
0051 
0052     private void refreshDevicesAction() {
0053         BackgroundService.ForceRefreshConnections(this);
0054 
0055         binding.devicesListLayout.refreshListLayout.setRefreshing(true);
0056         binding.devicesListLayout.refreshListLayout.postDelayed(() -> {
0057             binding.devicesListLayout.refreshListLayout.setRefreshing(false);
0058         }, 1500);
0059     }
0060 
0061     private void updateDeviceList() {
0062         final Intent intent = getIntent();
0063 
0064         String action = intent.getAction();
0065         if (!Intent.ACTION_SEND.equals(action) && !Intent.ACTION_SEND_MULTIPLE.equals(action)) {
0066             finish();
0067             return;
0068         }
0069 
0070         Collection<Device> devices = KdeConnect.getInstance().getDevices().values();
0071         final ArrayList<Device> devicesList = new ArrayList<>();
0072         final ArrayList<ListAdapter.Item> items = new ArrayList<>();
0073 
0074         SectionItem section = new SectionItem(getString(R.string.share_to));
0075         items.add(section);
0076 
0077         for (Device d : devices) {
0078             if (d.isReachable() && d.isPaired()) {
0079                 devicesList.add(d);
0080                 items.add(new EntryItemWithIcon(d.getName(), d.getIcon()));
0081                 section.isEmpty = false;
0082             }
0083         }
0084 
0085         binding.devicesListLayout.devicesList.setAdapter(new ListAdapter(ShareActivity.this, items));
0086         binding.devicesListLayout.devicesList.setOnItemClickListener((adapterView, view, i, l) -> {
0087             Device device = devicesList.get(i - 1); //NOTE: -1 because of the title!
0088             SharePlugin plugin = KdeConnect.getInstance().getDevicePlugin(device.getDeviceId(), SharePlugin.class);
0089             if (plugin != null) {
0090                 plugin.share(intent);
0091             }
0092             finish();
0093         });
0094     }
0095 
0096     @Override
0097     protected void onCreate(Bundle savedInstanceState) {
0098         super.onCreate(savedInstanceState);
0099 
0100         binding = ActivityShareBinding.inflate(getLayoutInflater());
0101         setContentView(binding.getRoot());
0102 
0103         setSupportActionBar(binding.toolbarLayout.toolbar);
0104         Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
0105         getSupportActionBar().setDisplayShowHomeEnabled(true);
0106 
0107         ActionBar actionBar = getSupportActionBar();
0108         binding.devicesListLayout.refreshListLayout.setOnRefreshListener(this::refreshDevicesAction);
0109         if (actionBar != null) {
0110             actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM);
0111         }
0112     }
0113 
0114     @Override
0115     protected void onStart() {
0116         super.onStart();
0117 
0118         final Intent intent = getIntent();
0119         final String deviceId = intent.getStringExtra("deviceId");
0120 
0121         if (deviceId != null) {
0122             SharePlugin plugin = KdeConnect.getInstance().getDevicePlugin(deviceId, SharePlugin.class);
0123             if (plugin != null) {
0124                 plugin.share(intent);
0125             }
0126             finish();
0127         } else {
0128             KdeConnect.getInstance().addDeviceListChangedCallback("ShareActivity", () -> runOnUiThread(this::updateDeviceList));
0129             BackgroundService.ForceRefreshConnections(this); // force a network re-discover
0130             updateDeviceList();
0131         }
0132     }
0133 
0134     @Override
0135     protected void onStop() {
0136         KdeConnect.getInstance().removeDeviceListChangedCallback("ShareActivity");
0137         super.onStop();
0138     }
0139 }