JLChen
2021-01-04 ed86fe7ed952bb2151aac8841134246bbde152c9
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
/*
 * Copyright (c) 2010-2019 Belledonne Communications SARL.
 *
 * This file is part of linphone-android
 * (see https://www.linphone.org).
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.linphone.settings;
 
import android.Manifest;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import androidx.annotation.Nullable;
import org.linphone.R;
import org.linphone.activities.MainActivity;
import org.linphone.compatibility.Compatibility;
import org.linphone.core.tools.Log;
import org.linphone.utils.LinphoneUtils;
 
public class SettingsActivity extends MainActivity {
    private static final int PERMISSIONS_REQUEST_OVERLAY = 206;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mOnBackPressGoHome = false;
        mAlwaysHideTabBar = true;
    }
 
    @Override
    protected void onStart() {
        super.onStart();
 
        Fragment currentFragment = getFragmentManager().findFragmentById(R.id.fragmentContainer);
        if (currentFragment == null) {
            if (getIntent() != null && getIntent().getExtras() != null) {
                Bundle extras = getIntent().getExtras();
                if (isTablet() || !extras.containsKey("Account")) {
                    showSettingsMenu();
                }
                handleIntentExtras(extras);
            } else {
                showSettingsMenu();
            }
        }
    }
 
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
 
        // Clean fragments stack upon return
        while (getFragmentManager().getBackStackEntryCount() > 0) {
            getFragmentManager().popBackStackImmediate();
        }
 
        handleIntentExtras(intent.getExtras());
    }
 
    @Override
    protected void onResume() {
        super.onResume();
 
        hideTabBar();
 
        int count = getFragmentManager().getBackStackEntryCount();
        if (count == 0) {
            showTopBarWithTitle(getString(R.string.settings));
        } else {
            FragmentManager.BackStackEntry entry =
                    getFragmentManager().getBackStackEntryAt(count - 1);
            showTopBarWithTitle(entry.getName());
        }
    }
 
    @Override
    public void goBack() {
        if (!isTablet()) {
            if (popBackStack()) {
                showTopBarWithTitle(getString(R.string.settings));
                return;
            }
        }
        super.goBack();
    }
 
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (requestCode == PERMISSIONS_REQUEST_OVERLAY) {
            if (Compatibility.canDrawOverlays(this)) {
                LinphonePreferences.instance().enableOverlay(true);
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
 
    @Override
    public void onRequestPermissionsResult(
            int requestCode, String[] permissions, int[] grantResults) {
        if (permissions.length <= 0) return;
        if (requestCode == MainActivity.FRAGMENT_SPECIFIC_PERMISSION) {
            for (int i = 0; i < permissions.length; i++) {
                Log.i(
                        "[Permission] "
                                + permissions[i]
                                + " is "
                                + (grantResults[i] == PackageManager.PERMISSION_GRANTED
                                        ? "granted"
                                        : "denied"));
                if (permissions[i].equals(Manifest.permission.CAMERA)
                        && grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                    LinphoneUtils.reloadVideoDevices();
                    LinphonePreferences.instance().setVideoPreviewEnabled(true);
                }
            }
        } else {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
 
    private void handleIntentExtras(Bundle extras) {
        if (extras != null && extras.containsKey("Account")) {
            int accountIndex = extras.getInt("Account");
            showAccountSettings(accountIndex, isTablet());
        }
    }
 
    private void showSettingsMenu() {
        Bundle extras = new Bundle();
        MenuSettingsFragment menuSettingsFragment = new MenuSettingsFragment();
        menuSettingsFragment.setArguments(extras);
        changeFragment(menuSettingsFragment, getString(R.string.settings), false);
        showTopBarWithTitle(getString(R.string.settings));
    }
 
    public void showSettings(Fragment fragment, String name) {
        changeFragment(fragment, name, true);
        showTopBarWithTitle(name);
    }
 
    public void showAccountSettings(int accountIndex, boolean isChild) {
        Bundle extras = new Bundle();
        extras.putInt("Account", accountIndex);
        AccountSettingsFragment accountSettingsFragment = new AccountSettingsFragment();
        accountSettingsFragment.setArguments(extras);
        changeFragment(accountSettingsFragment, getString(R.string.pref_sipaccount), isChild);
        showTopBarWithTitle(getString(R.string.pref_sipaccount));
    }
 
    public boolean checkAndRequestOverlayPermission() {
        Log.i(
                "[Permission] Draw overlays permission is "
                        + (Compatibility.canDrawOverlays(this) ? "granted" : "denied"));
        if (!Compatibility.canDrawOverlays(this)) {
            Log.i("[Permission] Asking for overlay");
            Intent intent =
                    new Intent(
                            Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                            Uri.parse("package:" + getPackageName()));
            startActivityForResult(intent, PERMISSIONS_REQUEST_OVERLAY);
            return false;
        }
        return true;
    }
}