summaryrefslogtreecommitdiff
path: root/app/src/main/java/com/javispedro/autobluetether/MainActivity.java
blob: 079985c08a1a9facd767aeaf56617b85c9a53067 (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
package com.javispedro.autobluetether;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

public class MainActivity extends AppCompatActivity {
    private final static String TAG = "MainActivity";

    private SystemSettingsFragment systemSettingsFragment;
    private DeviceFragment deviceFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentManager manager = getSupportFragmentManager();
        systemSettingsFragment = (SystemSettingsFragment) manager.findFragmentById(R.id.fragmentSystemSettings);
        deviceFragment = (DeviceFragment) manager.findFragmentById(R.id.fragmentDeviceList);
    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshSystemSettingsFragmentVisible();
        refreshDeviceList();
    }

    @Override
    protected void onDestroy() {
        deviceFragment = null;
        systemSettingsFragment = null;
        super.onDestroy();
    }

    private void refreshSystemSettingsFragmentVisible() {
        boolean can_write_settings = Settings.System.canWrite(this);
        Log.d(TAG, "can_write_settings: " + can_write_settings);
        setSystemSettingsFragmentVisible(!can_write_settings);
    }

    private void refreshDeviceList() {
        deviceFragment.refreshList();
    }

    private void setSystemSettingsFragmentVisible(boolean visible) {
        FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
        if (visible) {
            tx.show(systemSettingsFragment);
        } else {
            tx.hide(systemSettingsFragment);
        }
        tx.commit();
    }

    public void manageWriteSettings(View view) {
        Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
        intent.setData(Uri.parse("package:" + getPackageName()));
        startActivity(intent);
    }
}