Differential D14430 Diff 38901 src/org/kde/kdeconnect/Plugins/PresenterPlugin/PresenterActivity.java
Changeset View
Changeset View
Standalone View
Standalone View
src/org/kde/kdeconnect/Plugins/PresenterPlugin/PresenterActivity.java
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright 2014 Ahmed I. Khalil <ahmedibrahimkhali@gmail.com> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU General Public License as | ||||
6 | * published by the Free Software Foundation; either version 2 of | ||||
7 | * the License or (at your option) version 3 or any later version | ||||
8 | * accepted by the membership of KDE e.V. (or its successor approved | ||||
9 | * by the membership of KDE e.V.), which shall act as a proxy | ||||
10 | * defined in Section 14 of version 3 of the license. | ||||
11 | * | ||||
12 | * This program is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | * GNU General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU General Public License | ||||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | package org.kde.kdeconnect.Plugins.PresenterPlugin; | ||||
22 | | ||||
23 | import android.content.Context; | ||||
24 | import android.os.Build; | ||||
25 | import android.os.Bundle; | ||||
26 | import android.os.PowerManager; | ||||
27 | import android.support.v4.media.VolumeProviderCompat; | ||||
28 | import android.support.v4.media.session.MediaSessionCompat; | ||||
29 | import android.support.v4.media.session.PlaybackStateCompat; | ||||
30 | import android.support.v7.app.AppCompatActivity; | ||||
31 | import android.util.Log; | ||||
32 | import android.view.Menu; | ||||
33 | import android.view.MenuInflater; | ||||
34 | import android.view.MenuItem; | ||||
35 | | ||||
36 | import org.kde.kdeconnect.BackgroundService; | ||||
37 | import org.kde.kdeconnect.Device; | ||||
38 | import org.kde.kdeconnect.UserInterface.ThemeUtil; | ||||
39 | import org.kde.kdeconnect_tp.R; | ||||
40 | | ||||
41 | public class PresenterActivity extends AppCompatActivity { | ||||
42 | | ||||
43 | private MediaSessionCompat mMediaSession; | ||||
44 | | ||||
45 | PresenterPlugin plugin; | ||||
46 | | ||||
47 | @Override | ||||
48 | protected void onCreate(Bundle savedInstanceState) { | ||||
49 | super.onCreate(savedInstanceState); | ||||
50 | ThemeUtil.setUserPreferredTheme(this); | ||||
51 | | ||||
52 | setContentView(R.layout.activity_presenter); | ||||
53 | | ||||
54 | final String deviceId = getIntent().getStringExtra("deviceId"); | ||||
55 | | ||||
56 | BackgroundService.RunCommand(this, service -> { | ||||
57 | Device device = service.getDevice(deviceId); | ||||
58 | | ||||
59 | plugin = device.getPlugin(PresenterPlugin.class); | ||||
60 | if (plugin == null) { | ||||
61 | Log.e("PresenterActivity", "device has no presenter plugin!"); | ||||
62 | return; | ||||
63 | } | ||||
64 | | ||||
65 | runOnUiThread(() -> { | ||||
66 | | ||||
67 | findViewById(R.id.next_button).setOnClickListener(v -> { | ||||
68 | plugin.sendNext(); | ||||
69 | }); | ||||
70 | | ||||
71 | findViewById(R.id.previous_button).setOnClickListener(v -> { | ||||
72 | plugin.sendPrevious(); | ||||
73 | }); | ||||
74 | | ||||
75 | | ||||
76 | }); | ||||
77 | }); | ||||
78 | | ||||
79 | } | ||||
80 | @Override | ||||
81 | public boolean onCreateOptionsMenu(Menu menu) { | ||||
82 | MenuInflater inflater = getMenuInflater(); | ||||
83 | inflater.inflate(R.menu.menu_presenter, menu); | ||||
84 | return true; | ||||
85 | } | ||||
86 | | ||||
87 | @Override | ||||
88 | public boolean onOptionsItemSelected(MenuItem item) { | ||||
89 | switch(item.getItemId()) { | ||||
90 | case R.id.fullscreen: | ||||
91 | plugin.sendFullscreen(); | ||||
92 | return true; | ||||
93 | case R.id.exit_presentation: | ||||
94 | plugin.sendEsc(); | ||||
95 | return true; | ||||
96 | default: | ||||
97 | return super.onContextItemSelected(item); | ||||
98 | } | ||||
99 | } | ||||
100 | | ||||
101 | @Override | ||||
102 | protected void onStart() { | ||||
103 | super.onStart(); | ||||
104 | BackgroundService.addGuiInUseCounter(this); | ||||
105 | if (mMediaSession != null) { | ||||
106 | mMediaSession.setActive(true); | ||||
107 | return; | ||||
108 | } | ||||
109 | createMediaSession(); //Mediasession will keep | ||||
110 | } | ||||
111 | | ||||
112 | @Override | ||||
113 | protected void onStop() { | ||||
114 | super.onStop(); | ||||
115 | BackgroundService.removeGuiInUseCounter(this); | ||||
116 | | ||||
117 | if (mMediaSession != null) { | ||||
118 | PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE); | ||||
119 | boolean screenOn; | ||||
120 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) { | ||||
121 | screenOn = pm.isInteractive(); | ||||
122 | } else { | ||||
123 | screenOn = pm.isScreenOn(); | ||||
124 | } | ||||
125 | if (screenOn) { | ||||
126 | mMediaSession.release(); | ||||
127 | } // else we are in the lockscreen, keep the mediasession | ||||
128 | } | ||||
129 | } | ||||
130 | | ||||
131 | private void createMediaSession() { | ||||
132 | mMediaSession = new MediaSessionCompat(this, "kdeconnect"); | ||||
133 | | ||||
134 | mMediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | | ||||
135 | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS); | ||||
136 | mMediaSession.setPlaybackState(new PlaybackStateCompat.Builder() | ||||
137 | .setState(PlaybackStateCompat.STATE_PLAYING, 0, 0) | ||||
138 | .build()); | ||||
139 | mMediaSession.setPlaybackToRemote(getVolumeProvider()); | ||||
140 | mMediaSession.setActive(true); | ||||
141 | } | ||||
142 | | ||||
143 | private VolumeProviderCompat getVolumeProvider() { | ||||
144 | final int VOLUME_UP = 1; | ||||
145 | final int VOLUME_DOWN = -1; | ||||
146 | return new VolumeProviderCompat(VolumeProviderCompat.VOLUME_CONTROL_RELATIVE, 1, 0) { | ||||
147 | @Override | ||||
148 | public void onAdjustVolume(int direction) { | ||||
149 | if (direction == VOLUME_UP) { | ||||
150 | plugin.sendNext(); | ||||
151 | } | ||||
152 | else if (direction == VOLUME_DOWN) { | ||||
153 | plugin.sendPrevious(); | ||||
154 | } | ||||
155 | } | ||||
156 | }; | ||||
157 | } | ||||
158 | | ||||
159 | } | ||||
160 | |