2016-08-15 20:23:56 -07:00
|
|
|
package org.thoughtcrime.securesms;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.v7.app.AlertDialog;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.util.ExpirationUtil;
|
|
|
|
|
|
|
|
import cn.carbswang.android.numberpickerview.library.NumberPickerView;
|
2019-07-24 12:30:23 +10:00
|
|
|
import network.loki.messenger.R;
|
2016-08-15 20:23:56 -07:00
|
|
|
|
|
|
|
public class ExpirationDialog extends AlertDialog {
|
|
|
|
|
|
|
|
protected ExpirationDialog(Context context) {
|
|
|
|
super(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected ExpirationDialog(Context context, int theme) {
|
|
|
|
super(context, theme);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected ExpirationDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
|
|
|
|
super(context, cancelable, cancelListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void show(final Context context,
|
|
|
|
final int currentExpiration,
|
|
|
|
final @NonNull OnClickListener listener)
|
|
|
|
{
|
|
|
|
final View view = createNumberPickerView(context, currentExpiration);
|
|
|
|
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(context);
|
|
|
|
builder.setTitle(context.getString(R.string.ExpirationDialog_disappearing_messages));
|
|
|
|
builder.setView(view);
|
2018-02-26 09:58:18 -08:00
|
|
|
builder.setPositiveButton(android.R.string.ok, (dialog, which) -> {
|
|
|
|
int selected = ((NumberPickerView)view.findViewById(R.id.expiration_number_picker)).getValue();
|
|
|
|
listener.onClick(context.getResources().getIntArray(R.array.expiration_times)[selected]);
|
2016-08-15 20:23:56 -07:00
|
|
|
});
|
|
|
|
builder.setNegativeButton(android.R.string.cancel, null);
|
|
|
|
builder.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static View createNumberPickerView(final Context context, final int currentExpiration) {
|
|
|
|
final LayoutInflater inflater = LayoutInflater.from(context);
|
|
|
|
final View view = inflater.inflate(R.layout.expiration_dialog, null);
|
2018-02-26 09:58:18 -08:00
|
|
|
final NumberPickerView numberPickerView = view.findViewById(R.id.expiration_number_picker);
|
|
|
|
final TextView textView = view.findViewById(R.id.expiration_details);
|
2016-08-15 20:23:56 -07:00
|
|
|
final int[] expirationTimes = context.getResources().getIntArray(R.array.expiration_times);
|
|
|
|
final String[] expirationDisplayValues = new String[expirationTimes.length];
|
|
|
|
|
|
|
|
int selectedIndex = expirationTimes.length - 1;
|
|
|
|
|
|
|
|
for (int i=0;i<expirationTimes.length;i++) {
|
|
|
|
expirationDisplayValues[i] = ExpirationUtil.getExpirationDisplayValue(context, expirationTimes[i]);
|
|
|
|
|
|
|
|
if ((currentExpiration >= expirationTimes[i]) &&
|
|
|
|
(i == expirationTimes.length -1 || currentExpiration < expirationTimes[i+1])) {
|
|
|
|
selectedIndex = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
numberPickerView.setDisplayedValues(expirationDisplayValues);
|
|
|
|
numberPickerView.setMinValue(0);
|
|
|
|
numberPickerView.setMaxValue(expirationTimes.length-1);
|
|
|
|
|
2018-02-26 09:58:18 -08:00
|
|
|
NumberPickerView.OnValueChangeListener listener = (picker, oldVal, newVal) -> {
|
|
|
|
if (newVal == 0) {
|
|
|
|
textView.setText(R.string.ExpirationDialog_your_messages_will_not_expire);
|
|
|
|
} else {
|
|
|
|
textView.setText(context.getString(R.string.ExpirationDialog_your_messages_will_disappear_s_after_they_have_been_seen, picker.getDisplayedValues()[newVal]));
|
2016-08-15 20:23:56 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
numberPickerView.setOnValueChangedListener(listener);
|
|
|
|
numberPickerView.setValue(selectedIndex);
|
|
|
|
listener.onValueChange(numberPickerView, selectedIndex, selectedIndex);
|
|
|
|
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
public interface OnClickListener {
|
|
|
|
public void onClick(int expirationTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|