Search in sources :

Example 6 with BackgroundService

use of org.kde.kdeconnect.BackgroundService in project kdeconnect-android by KDE.

the class MprisActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mpris_control);
    targetPlayer = getIntent().getStringExtra("player");
    getIntent().removeExtra("player");
    deviceId = getIntent().getStringExtra("deviceId");
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    String interval_time_str = prefs.getString(getString(R.string.mpris_time_key), getString(R.string.mpris_time_default));
    final int interval_time = Integer.parseInt(interval_time_str);
    BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() {

        @Override
        public void onServiceStart(BackgroundService service) {
            service.addConnectionListener(connectionReceiver);
        }
    });
    connectToPlugin();
    findViewById(R.id.play_button).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() {

                @Override
                public void onServiceStart(BackgroundService service) {
                    Device device = service.getDevice(deviceId);
                    MprisPlugin mpris = device.getPlugin(MprisPlugin.class);
                    if (mpris == null)
                        return;
                    mpris.sendAction("PlayPause");
                }
            });
        }
    });
    findViewById(R.id.prev_button).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() {

                @Override
                public void onServiceStart(BackgroundService service) {
                    Device device = service.getDevice(deviceId);
                    MprisPlugin mpris = device.getPlugin(MprisPlugin.class);
                    if (mpris == null)
                        return;
                    mpris.sendAction("Previous");
                }
            });
        }
    });
    findViewById(R.id.rew_button).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() {

                @Override
                public void onServiceStart(BackgroundService service) {
                    Device device = service.getDevice(deviceId);
                    MprisPlugin mpris = device.getPlugin(MprisPlugin.class);
                    if (mpris == null)
                        return;
                    mpris.Seek(interval_time * -1);
                }
            });
        }
    });
    findViewById(R.id.ff_button).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() {

                @Override
                public void onServiceStart(BackgroundService service) {
                    Device device = service.getDevice(deviceId);
                    MprisPlugin mpris = device.getPlugin(MprisPlugin.class);
                    if (mpris == null)
                        return;
                    mpris.Seek(interval_time);
                }
            });
        }
    });
    findViewById(R.id.next_button).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() {

                @Override
                public void onServiceStart(BackgroundService service) {
                    Device device = service.getDevice(deviceId);
                    MprisPlugin mpris = device.getPlugin(MprisPlugin.class);
                    if (mpris == null)
                        return;
                    mpris.sendAction("Next");
                }
            });
        }
    });
    ((SeekBar) findViewById(R.id.volume_seek)).setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStopTrackingTouch(final SeekBar seekBar) {
            BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() {

                @Override
                public void onServiceStart(BackgroundService service) {
                    Device device = service.getDevice(deviceId);
                    MprisPlugin mpris = device.getPlugin(MprisPlugin.class);
                    if (mpris == null)
                        return;
                    mpris.setVolume(seekBar.getProgress());
                }
            });
        }
    });
    positionSeekUpdateRunnable = new Runnable() {

        @Override
        public void run() {
            final SeekBar positionSeek = (SeekBar) findViewById(R.id.positionSeek);
            BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() {

                @Override
                public void onServiceStart(BackgroundService service) {
                    Device device = service.getDevice(deviceId);
                    if (device != null) {
                        MprisPlugin mpris = device.getPlugin(MprisPlugin.class);
                        if (mpris != null) {
                            positionSeek.setProgress((int) (mpris.getPosition()));
                        }
                    }
                    positionSeekUpdateHandler.removeCallbacks(positionSeekUpdateRunnable);
                    positionSeekUpdateHandler.postDelayed(positionSeekUpdateRunnable, 1000);
                }
            });
        }
    };
    positionSeekUpdateHandler.postDelayed(positionSeekUpdateRunnable, 200);
    ((SeekBar) findViewById(R.id.positionSeek)).setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean byUser) {
            ((TextView) findViewById(R.id.progress_textview)).setText(milisToProgress(progress));
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            positionSeekUpdateHandler.removeCallbacks(positionSeekUpdateRunnable);
        }

        @Override
        public void onStopTrackingTouch(final SeekBar seekBar) {
            BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() {

                @Override
                public void onServiceStart(BackgroundService service) {
                    Device device = service.getDevice(deviceId);
                    MprisPlugin mpris = device.getPlugin(MprisPlugin.class);
                    if (mpris != null) {
                        mpris.setPosition(seekBar.getProgress());
                    }
                    positionSeekUpdateHandler.postDelayed(positionSeekUpdateRunnable, 200);
                }
            });
        }
    });
    findViewById(R.id.now_playing_textview).setSelected(true);
}
Also used : BackgroundService(org.kde.kdeconnect.BackgroundService) SeekBar(android.widget.SeekBar) SharedPreferences(android.content.SharedPreferences) Device(org.kde.kdeconnect.Device) TextView(android.widget.TextView) View(android.view.View) AdapterView(android.widget.AdapterView)

Example 7 with BackgroundService

use of org.kde.kdeconnect.BackgroundService in project kdeconnect-android by KDE.

the class RunCommandActivity method updateView.

private void updateView() {
    BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {

        @Override
        public void onServiceStart(final BackgroundService service) {
            final Device device = service.getDevice(deviceId);
            final RunCommandPlugin plugin = device.getPlugin(RunCommandPlugin.class);
            if (plugin == null) {
                Log.e("RunCommandActivity", "device has no runcommand plugin!");
                return;
            }
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    ListView view = (ListView) findViewById(R.id.listView1);
                    final ArrayList<ListAdapter.Item> commandItems = new ArrayList<>();
                    for (JSONObject obj : plugin.getCommandList()) {
                        try {
                            commandItems.add(new CommandEntry(obj.getString("name"), obj.getString("command"), obj.getString("key")));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    Collections.sort(commandItems, new Comparator<ListAdapter.Item>() {

                        @Override
                        public int compare(ListAdapter.Item lhs, ListAdapter.Item rhs) {
                            String lName = ((CommandEntry) lhs).getName();
                            String rName = ((CommandEntry) rhs).getName();
                            return lName.compareTo(rName);
                        }
                    });
                    ListAdapter adapter = new ListAdapter(RunCommandActivity.this, commandItems);
                    view.setAdapter(adapter);
                    view.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                            CommandEntry entry = (CommandEntry) commandItems.get(i);
                            plugin.runCommand(entry.getKey());
                        }
                    });
                }
            });
        }
    });
}
Also used : BackgroundService(org.kde.kdeconnect.BackgroundService) Device(org.kde.kdeconnect.Device) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) View(android.view.View) AdapterView(android.widget.AdapterView) ListView(android.widget.ListView) ListView(android.widget.ListView) JSONObject(org.json.JSONObject) AdapterView(android.widget.AdapterView) ListAdapter(org.kde.kdeconnect.UserInterface.List.ListAdapter)

Example 8 with BackgroundService

use of org.kde.kdeconnect.BackgroundService in project kdeconnect-android by KDE.

the class ShareActivity method updateComputerList.

private void updateComputerList() {
    final Intent intent = getIntent();
    String action = intent.getAction();
    if (!Intent.ACTION_SEND.equals(action) && !Intent.ACTION_SEND_MULTIPLE.equals(action)) {
        finish();
        return;
    }
    BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {

        @Override
        public void onServiceStart(final BackgroundService service) {
            Collection<Device> devices = service.getDevices().values();
            final ArrayList<Device> devicesList = new ArrayList<>();
            final ArrayList<ListAdapter.Item> items = new ArrayList<>();
            items.add(new SectionItem(getString(R.string.share_to)));
            for (Device d : devices) {
                if (d.isReachable() && d.isPaired()) {
                    devicesList.add(d);
                    items.add(new EntryItem(d.getName()));
                }
            }
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    ListView list = (ListView) findViewById(R.id.listView1);
                    list.setAdapter(new ListAdapter(ShareActivity.this, items));
                    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                            //NOTE: -1 because of the title!
                            Device device = devicesList.get(i - 1);
                            Bundle extras = intent.getExtras();
                            if (extras != null) {
                                if (extras.containsKey(Intent.EXTRA_STREAM)) {
                                    try {
                                        ArrayList<Uri> uriList;
                                        if (!Intent.ACTION_SEND.equals(intent.getAction())) {
                                            uriList = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
                                        } else {
                                            Uri uri = extras.getParcelable(Intent.EXTRA_STREAM);
                                            uriList = new ArrayList<>();
                                            uriList.add(uri);
                                        }
                                        SharePlugin.queuedSendUriList(getApplicationContext(), device, uriList);
                                    } catch (Exception e) {
                                        Log.e("ShareActivity", "Exception");
                                        e.printStackTrace();
                                    }
                                } else if (extras.containsKey(Intent.EXTRA_TEXT)) {
                                    String text = extras.getString(Intent.EXTRA_TEXT);
                                    String subject = extras.getString(Intent.EXTRA_SUBJECT);
                                    //Hack: Detect shared youtube videos, so we can open them in the browser instead of as text
                                    if (subject != null && subject.endsWith("YouTube")) {
                                        int index = text.indexOf(": http://youtu.be/");
                                        if (index > 0) {
                                            //Skip ": "
                                            text = text.substring(index + 2);
                                        }
                                    }
                                    boolean isUrl;
                                    try {
                                        new URL(text);
                                        isUrl = true;
                                    } catch (Exception e) {
                                        isUrl = false;
                                    }
                                    NetworkPackage np = new NetworkPackage(SharePlugin.PACKAGE_TYPE_SHARE_REQUEST);
                                    if (isUrl) {
                                        np.set("url", text);
                                    } else {
                                        np.set("text", text);
                                    }
                                    device.sendPackage(np);
                                }
                            }
                            finish();
                        }
                    });
                }
            });
        }
    });
}
Also used : BackgroundService(org.kde.kdeconnect.BackgroundService) ArrayList(java.util.ArrayList) Uri(android.net.Uri) URL(java.net.URL) SectionItem(org.kde.kdeconnect.UserInterface.List.SectionItem) ListView(android.widget.ListView) ListAdapter(org.kde.kdeconnect.UserInterface.List.ListAdapter) EntryItem(org.kde.kdeconnect.UserInterface.List.EntryItem) Device(org.kde.kdeconnect.Device) Bundle(android.os.Bundle) Intent(android.content.Intent) View(android.view.View) AdapterView(android.widget.AdapterView) ListView(android.widget.ListView) NetworkPackage(org.kde.kdeconnect.NetworkPackage) Collection(java.util.Collection) AdapterView(android.widget.AdapterView)

Aggregations

BackgroundService (org.kde.kdeconnect.BackgroundService)8 Device (org.kde.kdeconnect.Device)7 View (android.view.View)5 ListView (android.widget.ListView)4 ArrayList (java.util.ArrayList)4 AdapterView (android.widget.AdapterView)3 TextView (android.widget.TextView)3 ListAdapter (org.kde.kdeconnect.UserInterface.List.ListAdapter)3 SharedPreferences (android.content.SharedPreferences)2 Uri (android.net.Uri)2 SectionItem (org.kde.kdeconnect.UserInterface.List.SectionItem)2 ClipData (android.content.ClipData)1 Intent (android.content.Intent)1 Resources (android.content.res.Resources)1 Bundle (android.os.Bundle)1 PreferenceScreen (android.preference.PreferenceScreen)1 MenuItem (android.view.MenuItem)1 Button (android.widget.Button)1 SeekBar (android.widget.SeekBar)1 IOException (java.io.IOException)1