diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 69b413fe..82186447 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -9,6 +9,13 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@android:style/Theme.Material.Light.DarkActionBar"> + + + + + + + diff --git a/app/src/main/java/com/wireguard/android/BindingAdapters.java b/app/src/main/java/com/wireguard/android/BindingAdapters.java new file mode 100644 index 00000000..aa5b8c1e --- /dev/null +++ b/app/src/main/java/com/wireguard/android/BindingAdapters.java @@ -0,0 +1,38 @@ +package com.wireguard.android; + +import android.databinding.BindingAdapter; +import android.databinding.ObservableList; +import android.widget.ListView; + +/** + * Static methods for use by generated code in the Android data binding library. + */ + +public final class BindingAdapters { + @BindingAdapter({"items", "layout"}) + public static void listBinding(ListView view, ObservableList oldList, int oldLayoutId, + ObservableList newList, int newLayoutId) { + // Remove any existing binding when there is no new list. + if (newList == null) { + view.setAdapter(null); + return; + } + // The ListAdapter interface is not generic, so this cannot be checked. + @SuppressWarnings("unchecked") + ObservableListAdapter adapter = (ObservableListAdapter) view.getAdapter(); + // If the layout changes, any existing adapter must be replaced. + if (newLayoutId != oldLayoutId) + adapter = null; + // Add a new binding if there was none, or if it must be replaced due to a layout change. + if (adapter == null) { + adapter = new ObservableListAdapter<>(view.getContext(), newLayoutId, newList); + view.setAdapter(adapter); + } else if (newList != oldList) { + // Changing the list only requires modifying the existing adapter. + adapter.setList(newList); + } + } + + private BindingAdapters() { + } +} diff --git a/app/src/main/java/com/wireguard/android/ObservableListAdapter.java b/app/src/main/java/com/wireguard/android/ObservableListAdapter.java new file mode 100644 index 00000000..3b1cf5f8 --- /dev/null +++ b/app/src/main/java/com/wireguard/android/ObservableListAdapter.java @@ -0,0 +1,91 @@ +package com.wireguard.android; + +import android.content.Context; +import android.databinding.DataBindingUtil; +import android.databinding.ObservableList; +import android.databinding.ViewDataBinding; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.BaseAdapter; +import android.widget.ListAdapter; + +/** + * A generic ListAdapter backed by an ObservableList. + */ + +class ObservableListAdapter extends BaseAdapter implements ListAdapter { + private final int layoutId; + private final LayoutInflater layoutInflater; + private ObservableList list; + private final OnListChangedCallback> callback = new OnListChangedCallback<>(); + + ObservableListAdapter(Context context, int layoutId, ObservableList list) { + this.layoutInflater = LayoutInflater.from(context); + this.layoutId = layoutId; + setList(list); + } + + @Override + public int getCount() { + return list != null ? list.size() : 0; + } + + @Override + public T getItem(int position) { + return list != null ? list.get(position) : null; + } + + @Override + public long getItemId(int position) { + return position; + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + ViewDataBinding binding = DataBindingUtil.getBinding(convertView); + if (binding == null) + binding = DataBindingUtil.inflate(layoutInflater, layoutId, parent, false); + binding.setVariable(BR.item, getItem(position)); + binding.executePendingBindings(); + return binding.getRoot(); + } + + public void setList(ObservableList newList) { + if (list != null) + list.removeOnListChangedCallback(callback); + list = newList; + if (list != null) { + list.addOnListChangedCallback(callback); + } + } + + private class OnListChangedCallback> + extends ObservableList.OnListChangedCallback { + @Override + public void onChanged(L sender) { + ObservableListAdapter.this.notifyDataSetChanged(); + } + + @Override + public void onItemRangeChanged(L sender, int positionStart, int itemCount) { + ObservableListAdapter.this.notifyDataSetChanged(); + } + + @Override + public void onItemRangeInserted(L sender, int positionStart, int itemCount) { + ObservableListAdapter.this.notifyDataSetChanged(); + } + + @Override + public void onItemRangeMoved(L sender, int fromPosition, int toPosition, + int itemCount) { + ObservableListAdapter.this.notifyDataSetChanged(); + } + + @Override + public void onItemRangeRemoved(L sender, int positionStart, int itemCount) { + ObservableListAdapter.this.notifyDataSetChanged(); + } + } +} diff --git a/app/src/main/java/com/wireguard/android/ProfileListActivity.java b/app/src/main/java/com/wireguard/android/ProfileListActivity.java new file mode 100644 index 00000000..afa8a123 --- /dev/null +++ b/app/src/main/java/com/wireguard/android/ProfileListActivity.java @@ -0,0 +1,64 @@ +package com.wireguard.android; + +import android.app.Activity; +import android.databinding.DataBindingUtil; +import android.databinding.ObservableArrayList; +import android.databinding.ObservableList; +import android.os.AsyncTask; +import android.os.Bundle; +import android.util.Log; + +import com.wireguard.android.databinding.ProfileListActivityBinding; +import com.wireguard.config.Profile; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; + +public class ProfileListActivity extends Activity { + private final ObservableList profiles = new ObservableArrayList<>(); + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + final ProfileListActivityBinding binding = + DataBindingUtil.setContentView(this, R.layout.profile_list_activity); + binding.setProfiles(profiles); + new ProfileLoader().execute(getFilesDir().listFiles()); + } + + private class ProfileLoader extends AsyncTask> { + private static final String TAG = "WGProfileLoader"; + + @Override + protected ArrayList doInBackground(File... files) { + final ArrayList loadedProfiles = new ArrayList<>(); + for (File file : files) { + final String fileName = file.getName(); + final int suffixStart = fileName.lastIndexOf(".conf"); + if (suffixStart <= 0) { + Log.w(TAG, "Ignoring stray file " + fileName); + continue; + } + final Profile profile = new Profile(fileName.substring(0, suffixStart)); + try { + final FileInputStream inputStream = openFileInput(fileName); + profile.fromStream(inputStream); + loadedProfiles.add(profile); + } catch (IOException e) { + Log.w(TAG, "Failed to load profile from " + fileName, e); + } + if (isCancelled()) + break; + } + return loadedProfiles; + } + + @Override + protected void onPostExecute(ArrayList loadedProfiles) { + // FIXME: This should replace an existing profile if the name matches. + profiles.addAll(loadedProfiles); + } + } +} diff --git a/app/src/main/res/layout/profile_list_activity.xml b/app/src/main/res/layout/profile_list_activity.xml new file mode 100644 index 00000000..4e7b251f --- /dev/null +++ b/app/src/main/res/layout/profile_list_activity.xml @@ -0,0 +1,17 @@ + + + + + + + + + + diff --git a/app/src/main/res/layout/profile_list_item.xml b/app/src/main/res/layout/profile_list_item.xml new file mode 100644 index 00000000..aeafaf0f --- /dev/null +++ b/app/src/main/res/layout/profile_list_item.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + +