summaryrefslogtreecommitdiff
path: root/android/sdremote/mobile/src/main/java/org/libreoffice/impressremote/activity/ComputersActivity.java
blob: 4a2e0586200cdea1343144653f6576dc32dbfaab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package org.libreoffice.impressremote.activity;

import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AnimationUtils;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.tabs.TabLayout;

import org.libreoffice.impressremote.R;
import org.libreoffice.impressremote.adapter.ComputersPagerAdapter;
import org.libreoffice.impressremote.fragment.ComputersFragment.Type;
import org.libreoffice.impressremote.util.Intents;

public class ComputersActivity extends AppCompatActivity {
    private static final int REQUEST_ENABLE_BT = 0;
    private static final String SELECT_BLUETOOTH = "SELECT_BLUETOOTH";
    private static final BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
    private static final boolean disableBTOnQuit = btAdapter != null && !btAdapter.isEnabled();
    private static boolean haveBluetoothPermissionConnect = false;
    private static boolean haveBluetoothPermissionScan = false;
    private TabLayout tabLayout;
    private static TabLayout.Tab btTab;
    private static TabLayout.Tab wifiTab;
    private FloatingActionButton btFab;
    private FloatingActionButton addFab;
    private final ComputersPagerAdapter computersPagerAdapter = new ComputersPagerAdapter(getSupportFragmentManager(), this);

    private final ActivityResultLauncher<String> requestPermissionBTConnect =
            registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
                // TODO add blurp about not being able to use BT stuff if not granted
                haveBluetoothPermissionConnect = isGranted;
                init();
            });

    // requesting the connect permission actually asks for the nearby devices permission group,
    // requests for the scan permission are then automatically granted without a prompt, but you
    // still have to explicitly ask for it before you can call any related methods
    private final ActivityResultLauncher<String> requestPermissionBTScan =
            registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
                haveBluetoothPermissionScan = isGranted;
                btFab.setClickable(isGranted);
                btFab.performClick();
            });

    public static boolean getHaveBTConnect() {
        return haveBluetoothPermissionConnect;
    }

    public static boolean getHaveBTScan() {
        return haveBluetoothPermissionScan;
    }

    @Override
    protected void onCreate(Bundle aSavedInstanceState) {
        super.onCreate(aSavedInstanceState);
        // the pre-Android 12 Bluetooth permissions have "normal" danger level and don't require
        // asking at runtime
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S || ContextCompat.checkSelfPermission(
                this, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED) {
            haveBluetoothPermissionConnect = true;
            init();
            // TODO: add else if (shouldShowRequestPermissionRationale(...)) {
            // without that the user is only prompted a single time/would have to clear permissions
            // in system settings to be prompted again.
        } else {
            requestPermissionBTConnect.launch(Manifest.permission.BLUETOOTH_CONNECT);
        }
    }

    private void init() {
        setContentView(R.layout.activity_computers);

        Toolbar toolbar = (Toolbar) findViewById(R.id.computers_toolbar);
        setSupportActionBar(toolbar);
        // Looks hacky but it seems to be the best way to set activity’s title
        // different to application’s label. The other way is setting title
        // to intents filter but it shows wrong label for recent apps screen then.
        assert toolbar != null;
        toolbar.setTitle(R.string.title_computers);
        computersPagerAdapter.reset();
        computersPagerAdapter.addFragment(Type.BLUETOOTH);
        computersPagerAdapter.addFragment(Type.WIFI);

        ViewPager aComputersPager = (ViewPager) findViewById(R.id.pager_computers);
        assert aComputersPager != null;
        aComputersPager.setAdapter(computersPagerAdapter);
        tabLayout = (TabLayout) findViewById(R.id.pager_computers_tabs);
        assert tabLayout != null;
        tabLayout.setupWithViewPager(aComputersPager);

        btTab = tabLayout.getTabAt(0);
        wifiTab = tabLayout.getTabAt(1);
        assert wifiTab != null;
        wifiTab.select();

        if (btAdapter == null || !haveBluetoothPermissionConnect) {
            computersPagerAdapter.removeFragment(Type.BLUETOOTH);
        }

        tabLayout.setOnTabSelectedListener(
            new TabLayout.ViewPagerOnTabSelectedListener(aComputersPager) {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    super.onTabSelected(tab);
                    if (btAdapter != null && haveBluetoothPermissionConnect
                            && tab.getPosition() == btTab.getPosition()
                            && !btAdapter.isEnabled()) {
                        startActivityForResult(
                                new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE),
                                REQUEST_ENABLE_BT);
                    }
                }
            }
        );
        btFab = (FloatingActionButton) findViewById(R.id.btFab);
        btFab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btFab.clearAnimation();
                if (btAdapter == null) {
                    return;
                }
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && ContextCompat.checkSelfPermission(v.getContext(),
                        Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {
                    requestPermissionBTScan.launch(Manifest.permission.BLUETOOTH_SCAN);
                    return;
                } else {
                    haveBluetoothPermissionScan = true;
                }
                if (btAdapter.isDiscovering()) {
                    btAdapter.cancelDiscovery();
                } else if (btAdapter.startDiscovery()) {
                    // workaround, see https://code.google.com/p/android/issues/detail?id=175696#c6
                    // when not delayed animation won't show on pre-lollipop unless switching tabs
                    btFab.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            btFab.startAnimation(
                                    AnimationUtils.loadAnimation(getApplication(), R.anim.fabalpha));
                        }
                    }, 50);
                }
            }
        });

        addFab = (FloatingActionButton) findViewById(R.id.addFab);
        addFab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSupportFragmentManager().getFragments().get(0).startActivityForResult(
                        Intents.buildComputerCreationIntent(tabLayout.getContext()),
                        Intents.RequestCodes.CREATE_SERVER);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_ENABLE_BT) {
            if (resultCode != RESULT_OK) {
                wifiTab.select();
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu aMenu) {
        getMenuInflater().inflate(R.menu.menu_action_bar_computers, aMenu);
        // ComputerFragment uses invalidateOptionsMenu when BT-discovery broadcast is received
        toggleFab(tabLayout.getSelectedTabPosition());
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem aMenuItem) {
        int itemId = aMenuItem.getItemId();
        if (itemId == R.id.menu_settings) {
            startActivity(Intents.buildSettingsIntent(this));
            return true;
        } else if (itemId == R.id.menu_requirements) {
            startActivity(Intents.buildRequirementsIntent(this));
            return true;
        }
        return super.onOptionsItemSelected(aMenuItem);
    }

    private void toggleFab(int pos) {
        if(pos == btTab.getPosition()) {
            addFab.hide(new FloatingActionButton.OnVisibilityChangedListener() {
                @Override
                public void onHidden(FloatingActionButton fab) {
                    if( btAdapter == null) {
                        wifiTab.select();
                        return;
                    }
                    btFab.setClickable(true);
                    btFab.clearAnimation();

                    if (haveBluetoothPermissionScan && btAdapter.isDiscovering()) {
                        btFab.startAnimation(
                                AnimationUtils.loadAnimation(getApplication(), R.anim.fabalpha));
                    }
                    btFab.show();
                }
            });
        } else {
            btFab.setClickable(false);
            btFab.hide(new FloatingActionButton.OnVisibilityChangedListener() {
                @Override
                public void onHidden(FloatingActionButton fab) {
                    addFab.show();
                }
            });
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if(btTab == null) {
            return;
        }
        SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putBoolean(SELECT_BLUETOOTH, btTab.getPosition() == tabLayout.getSelectedTabPosition());
        editor.apply();
    }

    @Override
    protected void onStart() {
        super.onStart();

        SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
        if (haveBluetoothPermissionConnect && btTab.getPosition() != TabLayout.Tab.INVALID_POSITION
            && sharedPref.getBoolean(SELECT_BLUETOOTH, btAdapter != null)) {
            btTab.select();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (isFinishing() && disableBTOnQuit && haveBluetoothPermissionConnect) {
            btAdapter.disable();
        }
    }
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */