Differential D14430 Diff 38621 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.content.SharedPreferences; | ||||
25 | import android.media.AudioManager; | ||||
26 | import android.os.Build; | ||||
27 | import android.os.Bundle; | ||||
28 | import android.os.PowerManager; | ||||
29 | import android.preference.PreferenceManager; | ||||
30 | import android.support.v4.media.VolumeProviderCompat; | ||||
31 | import android.support.v4.media.session.MediaSessionCompat; | ||||
32 | import android.support.v4.media.session.PlaybackStateCompat; | ||||
33 | import android.support.v7.app.AppCompatActivity; | ||||
34 | import android.util.Log; | ||||
35 | import android.view.GestureDetector; | ||||
36 | import android.view.HapticFeedbackConstants; | ||||
37 | import android.view.Menu; | ||||
38 | import android.view.MenuInflater; | ||||
39 | import android.view.MenuItem; | ||||
40 | import android.view.MotionEvent; | ||||
41 | import android.view.View; | ||||
42 | import android.view.inputmethod.InputMethodManager; | ||||
43 | | ||||
44 | import org.kde.kdeconnect.BackgroundService; | ||||
45 | import org.kde.kdeconnect.Device; | ||||
46 | import org.kde.kdeconnect.Plugins.MousePadPlugin.KeyListenerView; | ||||
47 | import org.kde.kdeconnect.Plugins.MousePadPlugin.MousePadGestureDetector; | ||||
48 | import org.kde.kdeconnect.Plugins.PresenterPlugin.PresenterPlugin; | ||||
49 | import org.kde.kdeconnect.UserInterface.ThemeUtil; | ||||
50 | import org.kde.kdeconnect_tp.R; | ||||
51 | | ||||
52 | public class PresenterActivity extends AppCompatActivity { | ||||
53 | | ||||
54 | private MediaSessionCompat mMediaSession; | ||||
55 | | ||||
56 | PresenterPlugin plugin; | ||||
57 | | ||||
58 | @Override | ||||
59 | protected void onCreate(Bundle savedInstanceState) { | ||||
60 | super.onCreate(savedInstanceState); | ||||
61 | ThemeUtil.setUserPreferredTheme(this); | ||||
62 | | ||||
63 | setContentView(R.layout.activity_presenter); | ||||
64 | | ||||
65 | final String deviceId = getIntent().getStringExtra("deviceId"); | ||||
66 | | ||||
67 | BackgroundService.RunCommand(this, service -> { | ||||
68 | Device device = service.getDevice(deviceId); | ||||
69 | | ||||
70 | plugin = device.getPlugin(PresenterPlugin.class); | ||||
71 | if (plugin == null) { | ||||
72 | Log.e("PresenterActivity", "device has no presenter plugin!"); | ||||
73 | return; | ||||
74 | } | ||||
75 | | ||||
76 | runOnUiThread(() -> { | ||||
77 | | ||||
78 | findViewById(R.id.next_button).setOnClickListener(v -> { | ||||
79 | plugin.sendNext(); | ||||
80 | }); | ||||
81 | | ||||
82 | findViewById(R.id.previous_button).setOnClickListener(v -> { | ||||
83 | plugin.sendPrevious(); | ||||
84 | }); | ||||
85 | | ||||
86 | findViewById(R.id.fullscreen_button).setOnClickListener(v -> { | ||||
87 | plugin.sendFullscreen(); | ||||
88 | }); | ||||
89 | | ||||
90 | }); | ||||
91 | }); | ||||
92 | | ||||
93 | } | ||||
94 | | ||||
95 | @Override | ||||
96 | protected void onStart() { | ||||
97 | super.onStart(); | ||||
98 | BackgroundService.addGuiInUseCounter(this); | ||||
99 | if (mMediaSession != null) { | ||||
100 | mMediaSession.setActive(true); | ||||
101 | return; | ||||
102 | } | ||||
103 | createMediaSession(); //Mediasession will keep | ||||
104 | } | ||||
105 | | ||||
106 | @Override | ||||
107 | protected void onStop() { | ||||
108 | super.onStop(); | ||||
109 | BackgroundService.removeGuiInUseCounter(this); | ||||
110 | | ||||
111 | if (mMediaSession != null) { | ||||
112 | PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE); | ||||
113 | boolean screenOn; | ||||
114 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) { | ||||
115 | screenOn = pm.isInteractive(); | ||||
116 | } else { | ||||
117 | screenOn = pm.isScreenOn(); | ||||
118 | } | ||||
119 | if (screenOn) { | ||||
120 | mMediaSession.release(); | ||||
121 | } // else we are in the lockscreen, keep the mediasession | ||||
122 | } | ||||
123 | } | ||||
124 | | ||||
125 | private void createMediaSession() { | ||||
126 | mMediaSession = new MediaSessionCompat(this, "kdeconnect"); | ||||
127 | | ||||
128 | mMediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | | ||||
129 | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS); | ||||
130 | mMediaSession.setPlaybackState(new PlaybackStateCompat.Builder() | ||||
131 | .setState(PlaybackStateCompat.STATE_PLAYING, 0, 0) | ||||
132 | .build()); | ||||
133 | mMediaSession.setPlaybackToRemote(getVolumeProvider()); | ||||
134 | mMediaSession.setActive(true); | ||||
135 | } | ||||
136 | | ||||
137 | private VolumeProviderCompat getVolumeProvider() { | ||||
138 | final int VOLUME_UP = 1; | ||||
139 | final int VOLUME_DOWN = -1; | ||||
140 | return new VolumeProviderCompat(VolumeProviderCompat.VOLUME_CONTROL_RELATIVE, 1, 0) { | ||||
141 | @Override | ||||
142 | public void onAdjustVolume(int direction) { | ||||
143 | if (direction == VOLUME_UP) { | ||||
144 | plugin.sendNext(); | ||||
145 | } | ||||
146 | else if (direction == VOLUME_DOWN) { | ||||
147 | plugin.sendPrevious(); | ||||
148 | } | ||||
149 | } | ||||
150 | }; | ||||
151 | } | ||||
152 | | ||||
153 | } | ||||
154 | |