That fuck shit the fascists are using
at master 227 lines 8.5 kB view raw
1package org.tm.archive.preferences; 2 3import android.os.Bundle; 4import android.view.LayoutInflater; 5import android.view.View; 6import android.view.ViewGroup; 7import android.view.WindowManager; 8import android.widget.EditText; 9import android.widget.TextView; 10 11import androidx.annotation.NonNull; 12import androidx.annotation.Nullable; 13import androidx.appcompat.widget.SwitchCompat; 14import androidx.core.app.ShareCompat; 15import androidx.fragment.app.Fragment; 16import androidx.lifecycle.ViewModelProvider; 17 18import com.google.android.material.dialog.MaterialAlertDialogBuilder; 19 20import org.tm.archive.R; 21import org.tm.archive.contactshare.SimpleTextWatcher; 22import org.tm.archive.keyvalue.SignalStore; 23import org.tm.archive.util.CommunicationActions; 24import org.signal.core.util.concurrent.LifecycleDisposable; 25import org.tm.archive.util.SignalProxyUtil; 26import org.tm.archive.util.Util; 27import org.tm.archive.util.ViewUtil; 28import org.tm.archive.util.views.CircularProgressMaterialButton; 29import org.tm.archive.util.views.LearnMoreTextView; 30import org.whispersystems.signalservice.api.websocket.WebSocketConnectionState; 31import org.whispersystems.signalservice.internal.configuration.SignalProxy; 32 33import java.util.Optional; 34 35public class EditProxyFragment extends Fragment { 36 37 private SwitchCompat proxySwitch; 38 private EditText proxyText; 39 private TextView proxyTitle; 40 private TextView proxyStatus; 41 private View shareButton; 42 private CircularProgressMaterialButton saveButton; 43 private EditProxyViewModel viewModel; 44 private LifecycleDisposable lifecycleDisposable; 45 46 public static EditProxyFragment newInstance() { 47 return new EditProxyFragment(); 48 } 49 50 @Override 51 public @Nullable View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 52 return inflater.inflate(R.layout.edit_proxy_fragment, container, false); 53 } 54 55 @Override 56 public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { 57 this.proxySwitch = view.findViewById(R.id.edit_proxy_switch); 58 this.proxyTitle = view.findViewById(R.id.edit_proxy_address_title); 59 this.proxyText = view.findViewById(R.id.edit_proxy_host); 60 this.proxyStatus = view.findViewById(R.id.edit_proxy_status); 61 this.saveButton = view.findViewById(R.id.edit_proxy_save); 62 this.shareButton = view.findViewById(R.id.edit_proxy_share); 63 64 lifecycleDisposable = new LifecycleDisposable(); 65 lifecycleDisposable.bindTo(getViewLifecycleOwner()); 66 67 proxyText.addTextChangedListener(new SimpleTextWatcher() { 68 @Override 69 public void onTextChanged(String text) { 70 onProxyTextChanged(text); 71 } 72 }); 73 74 this.proxyText.setText(Optional.ofNullable(SignalStore.proxy().getProxy()).map(SignalProxy::getHost).orElse("")); 75 this.proxySwitch.setChecked(SignalStore.proxy().isProxyEnabled()); 76 77 initViewModel(); 78 79 saveButton.setOnClickListener(v -> onSaveClicked()); 80 shareButton.setOnClickListener(v -> onShareClicked()); 81 proxySwitch.setOnCheckedChangeListener((buttonView, isChecked) -> viewModel.onToggleProxy(isChecked, proxyText.getText().toString())); 82 83 LearnMoreTextView description = view.findViewById(R.id.edit_proxy_switch_title_description); 84 description.setLearnMoreVisible(true); 85 description.setOnLinkClickListener(v -> CommunicationActions.openBrowserLink(requireContext(), "https://www.telemessage.com/faqs/")); //**TM_SA**// change the link 86 87 requireActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); 88 } 89 90 @Override 91 public void onResume() { 92 super.onResume(); 93 94 SignalProxyUtil.startListeningToWebsocket(); 95 } 96 97 private void initViewModel() { 98 viewModel = new ViewModelProvider(this).get(EditProxyViewModel.class); 99 100 lifecycleDisposable.addAll( 101 viewModel.getUiState().subscribe(this::presentUiState), 102 viewModel.getProxyState().subscribe(this::presentProxyState), 103 viewModel.getEvents().subscribe(this::presentEvent), 104 viewModel.getSaveState().subscribe(this::presentSaveState) 105 ); 106 } 107 108 private void presentUiState(@NonNull EditProxyViewModel.UiState uiState) { 109 switch (uiState) { 110 case ALL_ENABLED: 111 proxyText.setEnabled(true); 112 proxyText.setAlpha(1); 113 saveButton.setEnabled(true); 114 saveButton.setAlpha(1); 115 proxyTitle.setAlpha(1); 116 onProxyTextChanged(proxyText.getText().toString()); 117 break; 118 case ALL_DISABLED: 119 proxyText.setEnabled(false); 120 proxyText.setAlpha(0.5f); 121 saveButton.setEnabled(false); 122 saveButton.setAlpha(0.5f); 123 shareButton.setEnabled(false); 124 shareButton.setAlpha(0.5f); 125 proxyTitle.setAlpha(0.5f); 126 proxyStatus.setVisibility(View.INVISIBLE); 127 break; 128 } 129 } 130 131 private void presentProxyState(@NonNull WebSocketConnectionState proxyState) { 132 if (SignalStore.proxy().getProxy() != null) { 133 switch (proxyState) { 134 case DISCONNECTED: 135 case DISCONNECTING: 136 case CONNECTING: 137 proxyStatus.setText(R.string.preferences_connecting_to_proxy); 138 proxyStatus.setTextColor(getResources().getColor(R.color.signal_text_secondary)); 139 break; 140 case CONNECTED: 141 proxyStatus.setText(R.string.preferences_connected_to_proxy); 142 proxyStatus.setTextColor(getResources().getColor(R.color.signal_accent_green)); 143 break; 144 case AUTHENTICATION_FAILED: 145 case FAILED: 146 proxyStatus.setText(R.string.preferences_connection_failed); 147 proxyStatus.setTextColor(getResources().getColor(R.color.signal_alert_primary)); 148 break; 149 } 150 } else { 151 proxyStatus.setText(""); 152 } 153 } 154 155 private void presentEvent(@NonNull EditProxyViewModel.Event event) { 156 switch (event) { 157 case PROXY_SUCCESS: 158 proxyStatus.setVisibility(View.VISIBLE); 159 proxyText.setText(Optional.ofNullable(SignalStore.proxy().getProxy()).map(SignalProxy::getHost).orElse("")); 160 new MaterialAlertDialogBuilder(requireContext()) 161 .setTitle(R.string.preferences_success) 162 .setMessage(R.string.preferences_you_are_connected_to_the_proxy) 163 .setPositiveButton(android.R.string.ok, (d, i) -> { 164 requireActivity().onBackPressed(); 165 d.dismiss(); 166 }) 167 .show(); 168 break; 169 case PROXY_FAILURE: 170 proxyStatus.setVisibility(View.INVISIBLE); 171 proxyText.setText(Optional.ofNullable(SignalStore.proxy().getProxy()).map(SignalProxy::getHost).orElse("")); 172 ViewUtil.focusAndMoveCursorToEndAndOpenKeyboard(proxyText); 173 new MaterialAlertDialogBuilder(requireContext()) 174 .setTitle(R.string.preferences_failed_to_connect) 175 .setMessage(R.string.preferences_couldnt_connect_to_the_proxy) 176 .setPositiveButton(android.R.string.ok, (d, i) -> d.dismiss()) 177 .show(); 178 break; 179 } 180 } 181 182 private void presentSaveState(@NonNull EditProxyViewModel.SaveState state) { 183 switch (state) { 184 case IDLE: 185 saveButton.cancelSpinning(); 186 break; 187 case IN_PROGRESS: 188 saveButton.setSpinning(); 189 break; 190 } 191 } 192 193 private void onSaveClicked() { 194 String text = proxyText.getText().toString(); 195 if (Util.isEmpty(text)) { 196 proxySwitch.setChecked(false); 197 } else { 198 viewModel.onSaveClicked(text); 199 } 200 } 201 202 private void onShareClicked() { 203 String link = SignalProxyUtil.generateProxyUrl(proxyText.getText().toString()); 204 ShareCompat.IntentBuilder.from(requireActivity()) 205 .setText(link) 206 .setType("text/plain") 207 .startChooser(); 208 } 209 210 private void onProxyTextChanged(@NonNull String text) { 211 if (Util.isEmpty(text)) { 212 shareButton.setEnabled(false); 213 shareButton.setAlpha(0.5f); 214 proxyStatus.setVisibility(View.INVISIBLE); 215 } else { 216 shareButton.setEnabled(true); 217 shareButton.setAlpha(1); 218 219 String trueHost = SignalProxyUtil.convertUserEnteredAddressToHost(proxyText.getText().toString()); 220 if (SignalStore.proxy().isProxyEnabled() && trueHost.equals(SignalStore.proxy().getProxyHost())) { 221 proxyStatus.setVisibility(View.VISIBLE); 222 } else { 223 proxyStatus.setVisibility(View.INVISIBLE); 224 } 225 } 226 } 227}