2017-08-13 14:24:03 +02:00
|
|
|
package com.wireguard.android;
|
|
|
|
|
2017-08-24 08:24:55 +02:00
|
|
|
import android.app.Activity;
|
2017-08-24 09:11:55 +02:00
|
|
|
import android.content.ClipData;
|
|
|
|
import android.content.ClipboardManager;
|
2017-08-13 14:24:03 +02:00
|
|
|
import android.content.Context;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.view.inputmethod.InputMethodManager;
|
2017-08-16 00:36:11 +02:00
|
|
|
import android.widget.Toast;
|
2017-08-13 14:24:03 +02:00
|
|
|
|
|
|
|
import com.wireguard.android.databinding.ConfigEditFragmentBinding;
|
|
|
|
import com.wireguard.config.Config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fragment for editing a WireGuard configuration.
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class ConfigEditFragment extends BaseConfigFragment {
|
2017-08-24 08:24:55 +02:00
|
|
|
private static final String KEY_MODIFIED_CONFIG = "modifiedConfig";
|
|
|
|
private static final String KEY_ORIGINAL_NAME = "originalName";
|
|
|
|
|
2017-08-24 09:11:55 +02:00
|
|
|
public static void copyPublicKey(final Context context, final String publicKey) {
|
|
|
|
if (publicKey == null || publicKey.isEmpty())
|
|
|
|
return;
|
|
|
|
final ClipboardManager clipboard =
|
|
|
|
(ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
|
|
|
|
final String description =
|
|
|
|
context.getResources().getString(R.string.public_key_description);
|
|
|
|
clipboard.setPrimaryClip(ClipData.newPlainText(description, publicKey));
|
|
|
|
final String message = context.getResources().getString(R.string.public_key_copied_message);
|
|
|
|
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
|
2017-08-24 08:24:55 +02:00
|
|
|
private Config localConfig;
|
|
|
|
private String originalName;
|
2017-08-13 14:24:03 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCurrentConfigChanged(final Config config) {
|
2017-08-24 08:24:55 +02:00
|
|
|
// Only discard modifications when the config *they are based on* changes.
|
|
|
|
if (config == null || config.getName().equals(originalName) || localConfig == null)
|
|
|
|
return;
|
2017-08-13 14:24:03 +02:00
|
|
|
localConfig.copyFrom(config);
|
2017-08-24 08:24:55 +02:00
|
|
|
originalName = config.getName();
|
2017-08-13 14:24:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(final Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
2017-08-24 08:24:55 +02:00
|
|
|
// Restore more saved information.
|
|
|
|
if (savedInstanceState != null) {
|
|
|
|
localConfig = savedInstanceState.getParcelable(KEY_MODIFIED_CONFIG);
|
|
|
|
originalName = savedInstanceState.getString(KEY_ORIGINAL_NAME);
|
|
|
|
} else if (getArguments() != null) {
|
|
|
|
final Bundle arguments = getArguments();
|
|
|
|
localConfig = arguments.getParcelable(KEY_MODIFIED_CONFIG);
|
|
|
|
originalName = arguments.getString(KEY_ORIGINAL_NAME);
|
|
|
|
}
|
|
|
|
if (localConfig == null) {
|
|
|
|
localConfig = new Config();
|
|
|
|
originalName = null;
|
|
|
|
}
|
|
|
|
onCurrentConfigChanged(getCurrentConfig());
|
2017-08-13 14:24:03 +02:00
|
|
|
setHasOptionsMenu(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
|
|
|
|
inflater.inflate(R.menu.config_edit, menu);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View onCreateView(final LayoutInflater inflater, final ViewGroup parent,
|
|
|
|
final Bundle savedInstanceState) {
|
|
|
|
final ConfigEditFragmentBinding binding =
|
|
|
|
ConfigEditFragmentBinding.inflate(inflater, parent, false);
|
|
|
|
binding.setConfig(localConfig);
|
|
|
|
return binding.getRoot();
|
|
|
|
}
|
|
|
|
|
2017-08-24 08:24:55 +02:00
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
|
|
|
// Reset changes to the config when the user cancels editing. See also the comment below.
|
|
|
|
if (isRemoving())
|
|
|
|
localConfig.copyFrom(getCurrentConfig());
|
|
|
|
}
|
|
|
|
|
2017-08-13 14:24:03 +02:00
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(final MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case R.id.menu_action_save:
|
|
|
|
saveConfig();
|
|
|
|
return true;
|
|
|
|
default:
|
2017-08-24 08:24:55 +02:00
|
|
|
return super.onOptionsItemSelected(item);
|
2017-08-13 14:24:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-24 08:24:55 +02:00
|
|
|
@Override
|
|
|
|
public void onSaveInstanceState(final Bundle outState) {
|
|
|
|
super.onSaveInstanceState(outState);
|
|
|
|
// When ConfigActivity unwinds the back stack, isRemoving() is true, so localConfig will be
|
|
|
|
// reset. Since outState is not serialized yet, it resets the saved config too. Avoid this
|
|
|
|
// by copying the local config. originalName is fine because it is replaced, not modified.
|
|
|
|
outState.putParcelable(KEY_MODIFIED_CONFIG, localConfig.copy());
|
|
|
|
outState.putString(KEY_ORIGINAL_NAME, originalName);
|
|
|
|
}
|
|
|
|
|
2017-08-13 14:24:03 +02:00
|
|
|
private void saveConfig() {
|
2017-08-24 08:24:55 +02:00
|
|
|
final VpnService service = VpnService.getInstance();
|
2017-08-16 00:36:11 +02:00
|
|
|
try {
|
|
|
|
if (getCurrentConfig() != null)
|
2017-08-24 08:24:55 +02:00
|
|
|
service.update(getCurrentConfig().getName(), localConfig);
|
2017-08-16 00:36:11 +02:00
|
|
|
else
|
2017-08-24 08:24:55 +02:00
|
|
|
service.add(localConfig);
|
2017-08-25 05:13:46 +02:00
|
|
|
} catch (final IllegalArgumentException | IllegalStateException e) {
|
2017-08-16 00:36:11 +02:00
|
|
|
Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
|
|
|
|
return;
|
|
|
|
}
|
2017-08-13 14:24:03 +02:00
|
|
|
// Hide the keyboard; it rarely goes away on its own.
|
2017-08-24 08:24:55 +02:00
|
|
|
final Activity activity = getActivity();
|
2017-08-13 14:24:03 +02:00
|
|
|
final View focusedView = activity.getCurrentFocus();
|
|
|
|
if (focusedView != null) {
|
|
|
|
final InputMethodManager inputManager =
|
|
|
|
(InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
|
|
inputManager.hideSoftInputFromWindow(focusedView.getWindowToken(),
|
|
|
|
InputMethodManager.HIDE_NOT_ALWAYS);
|
|
|
|
}
|
2017-08-16 00:36:11 +02:00
|
|
|
// Tell the activity to finish itself or go back to the detail view.
|
2017-08-24 08:24:55 +02:00
|
|
|
((BaseConfigActivity) activity).setIsEditing(false);
|
2017-08-13 14:24:03 +02:00
|
|
|
}
|
|
|
|
}
|