diff options
author | cyBerta <cyberta@riseup.net> | 2020-01-02 23:06:36 +0100 |
---|---|---|
committer | cyberta <cyberta@riseup.net> | 2020-01-24 10:36:03 -0600 |
commit | 921ee995ef0f0e7f2076ac3538fed289bcb82ba9 (patch) | |
tree | 349b3a3472f0e5436e3abb29d8955be1c8c51c7f /app/src/main/java/se/leap/bitmaskclient/fragments | |
parent | 87bdccc31f5fcd10e697d30225105ca40114582c (diff) |
implement basic UI for VPN tethering
Diffstat (limited to 'app/src/main/java/se/leap/bitmaskclient/fragments')
-rw-r--r-- | app/src/main/java/se/leap/bitmaskclient/fragments/TetheringDialog.java | 164 |
1 files changed, 135 insertions, 29 deletions
diff --git a/app/src/main/java/se/leap/bitmaskclient/fragments/TetheringDialog.java b/app/src/main/java/se/leap/bitmaskclient/fragments/TetheringDialog.java index 90e3c5a1..96f7f0d4 100644 --- a/app/src/main/java/se/leap/bitmaskclient/fragments/TetheringDialog.java +++ b/app/src/main/java/se/leap/bitmaskclient/fragments/TetheringDialog.java @@ -2,48 +2,113 @@ package se.leap.bitmaskclient.fragments; import android.app.Dialog; import android.content.Intent; -import android.os.Build; +import android.graphics.drawable.Drawable; import android.os.Bundle; +import android.provider.Settings; import android.support.annotation.NonNull; -import android.support.annotation.Nullable; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatDialogFragment; import android.support.v7.widget.AppCompatTextView; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.text.Spannable; +import android.text.SpannableString; +import android.text.method.LinkMovementMethod; +import android.text.style.ClickableSpan; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; -import android.widget.CheckBox; +import android.view.ViewGroup; import butterknife.ButterKnife; import butterknife.InjectView; import se.leap.bitmaskclient.R; -import se.leap.bitmaskclient.views.IconTextView; - -import static se.leap.bitmaskclient.utils.PreferenceHelper.saveShowAlwaysOnDialog; - +import se.leap.bitmaskclient.eip.EipCommand; +import se.leap.bitmaskclient.utils.PreferenceHelper; +import se.leap.bitmaskclient.views.IconCheckboxEntry; /** * Created by cyberta on 25.02.18. */ - - public class TetheringDialog extends AppCompatDialogFragment { public final static String TAG = TetheringDialog.class.getName(); - @InjectView(R.id.do_not_show_again) - CheckBox doNotShowAgainCheckBox; + @InjectView(R.id.tvTitle) + AppCompatTextView title; @InjectView(R.id.user_message) - IconTextView userMessage; + AppCompatTextView userMessage; - @InjectView(R.id.block_vpn_user_message) - AppCompatTextView blockVpnUserMessage; + @InjectView(R.id.selection_list_view) + RecyclerView selectionListView; + DialogListAdapter adapter; + private DialogListAdapter.ViewModel[] dataset; + public static class DialogListAdapter extends RecyclerView.Adapter<DialogListAdapter.ViewHolder> { - @Override - public void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); + interface OnItemClickListener { + void onItemClick(ViewModel item); + } + + private ViewModel[] dataSet; + private OnItemClickListener clickListener; + + public DialogListAdapter(ViewModel[] dataSet, OnItemClickListener clickListener) { + this.dataSet = dataSet; + this.clickListener = clickListener; + } + + @NonNull + @Override + public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { + IconCheckboxEntry v = new IconCheckboxEntry(viewGroup.getContext()); + return new ViewHolder(v); + } + + @Override + public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) { + viewHolder.bind(dataSet[i], clickListener); + } + + @Override + public int getItemCount() { + return dataSet.length; + } + + public static class ViewModel { + + public Drawable image; + public String text; + public boolean checked; + + ViewModel(Drawable image, String text, boolean checked) { + Log.d(TAG, "init ViewModel with string: " + text + " checked: " + checked); + + this.image = image; + this.text = text; + this.checked = checked; + } + } + + public static class ViewHolder extends RecyclerView.ViewHolder { + + ViewHolder(IconCheckboxEntry v) { + super(v); + } + + public void bind(ViewModel model, OnItemClickListener onClickListener) { + Log.d(TAG, "bind ViewModel"); + + ((IconCheckboxEntry) this.itemView).bind(model); + this.itemView.setOnClickListener(v -> { + model.checked = !model.checked; + ((IconCheckboxEntry) itemView).setChecked(model.checked); + onClickListener.onItemClick(model); + }); + } + } } @NonNull @@ -51,26 +116,67 @@ public class TetheringDialog extends AppCompatDialogFragment { public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); LayoutInflater inflater = getActivity().getLayoutInflater(); - View view = inflater.inflate(R.layout.d_checkbox_confirm, null); + View view = inflater.inflate(R.layout.d_list_selection, null); ButterKnife.inject(this, view); - userMessage.setIcon(R.drawable.ic_settings); - userMessage.setText(getString(R.string.always_on_vpn_user_message)); + title.setText(R.string.tethering); + userMessage.setMovementMethod(LinkMovementMethod.getInstance()); + userMessage.setLinkTextColor(getContext().getResources().getColor(R.color.colorPrimary)); + userMessage.setText(createUserMessage()); + + initDataset(); + adapter = new DialogListAdapter(dataset, this::onItemClick); + selectionListView.setAdapter(adapter); + selectionListView.setLayoutManager(new LinearLayoutManager(getActivity())); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - blockVpnUserMessage.setVisibility(View.VISIBLE); - } builder.setView(view) .setPositiveButton(android.R.string.ok, (dialog, id) -> { - if (doNotShowAgainCheckBox.isChecked()) { - saveShowAlwaysOnDialog(getContext(), false); - } - Intent intent = new Intent("android.net.vpn.SETTINGS"); - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - startActivity(intent); + PreferenceHelper.wifiTethering(getContext(), dataset[0].checked); + PreferenceHelper.usbTethering(getContext(), dataset[1].checked); + PreferenceHelper.bluetoothTethering(getContext(), dataset[2].checked); + EipCommand.configureTethering(getContext()); }) .setNegativeButton(R.string.cancel, (dialog, id) -> dialog.cancel()); return builder.create(); } + + public void onItemClick(DialogListAdapter.ViewModel item) { + + } + + private CharSequence createUserMessage() { + String tetheringMessage = getString(R.string.tethering_message); + String systemSettings = getString(R.string.tethering_system_settings); + String systemSettingsMessage = getString(R.string.tethering_enabled_message, systemSettings); + String wholeMessage = systemSettingsMessage + "\n\n" + tetheringMessage; + int startIndex = wholeMessage.indexOf(systemSettings, 0); + int endIndex = startIndex + systemSettings.length(); + + Spannable spannable = new SpannableString(wholeMessage); + spannable.setSpan(new ClickableSpan() { + @Override + public void onClick(@NonNull View widget) { + Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS); + startActivity(intent); + } + }, startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + + return spannable; + } + + private void initDataset() { + dataset = new DialogListAdapter.ViewModel[] { + new DialogListAdapter.ViewModel(getContext().getResources().getDrawable(R.drawable.ic_wifi), + getContext().getString(R.string.tethering_wifi), + PreferenceHelper.getWifiTethering(getContext())), + new DialogListAdapter.ViewModel(getContext().getResources().getDrawable(R.drawable.ic_usb), + getContext().getString(R.string.tethering_usb), + PreferenceHelper.getUsbTethering(getContext())), + new DialogListAdapter.ViewModel(getContext().getResources().getDrawable(R.drawable.ic_bluetooth), + getContext().getString(R.string.tethering_bluetooth), + PreferenceHelper.getBluetoothTethering(getContext())) + }; + } + } |