diff --git a/src/biz/bokhorst/xprivacy/WhitelistAdapter.java b/src/biz/bokhorst/xprivacy/WhitelistAdapter.java new file mode 100644 index 000000000..3dc40a81b --- /dev/null +++ b/src/biz/bokhorst/xprivacy/WhitelistAdapter.java @@ -0,0 +1,95 @@ +package biz.bokhorst.xprivacy; + +import java.util.ArrayList; +import java.util.Map; +import java.util.TreeMap; + +import android.annotation.SuppressLint; +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.view.View.OnClickListener; +import android.widget.ArrayAdapter; +import android.widget.CheckedTextView; +import android.widget.ImageView; + +@SuppressLint("DefaultLocale") +public class WhitelistAdapter extends ArrayAdapter { + private String mSelectedType; + private int mUid; + private Map> mMapWhitelists; + private LayoutInflater mInflater; + + public WhitelistAdapter(Context context, int resource, int uid, Map> mapWhitelists) { + super(context, resource, new ArrayList()); + mUid = uid; + mMapWhitelists = mapWhitelists; + mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + } + + public void setType(String selectedType) { + mSelectedType = selectedType; + this.clear(); + if (mMapWhitelists.containsKey(selectedType)) + this.addAll(mMapWhitelists.get(selectedType).keySet()); + } + + private class ViewHolder { + private View row; + public CheckedTextView ctvName; + public ImageView imgDelete; + + public ViewHolder(View theRow, int thePosition) { + row = theRow; + ctvName = (CheckedTextView) row.findViewById(R.id.cbName); + imgDelete = (ImageView) row.findViewById(R.id.imgDelete); + } + } + + @Override + @SuppressLint("InflateParams") + public View getView(int position, View convertView, ViewGroup parent) { + final ViewHolder holder; + if (convertView == null) { + convertView = mInflater.inflate(R.layout.whitelistentry, null); + holder = new ViewHolder(convertView, position); + convertView.setTag(holder); + } else + holder = (ViewHolder) convertView.getTag(); + + // Set data + final String name = this.getItem(position); + holder.ctvName.setText(name); + holder.ctvName.setChecked(mMapWhitelists.get(mSelectedType).get(name)); + + final boolean wnomod = PrivacyManager.getSettingBool(mUid, PrivacyManager.cSettingWhitelistNoModify, false); + + holder.ctvName.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View view) { + // Toggle white/black list entry + holder.ctvName.toggle(); + boolean isChecked = holder.ctvName.isChecked(); + mMapWhitelists.get(mSelectedType).put(name, isChecked); + PrivacyManager.setSetting(mUid, mSelectedType, name, Boolean.toString(isChecked)); + if (!wnomod) + PrivacyManager.updateState(mUid); + } + }); + + holder.imgDelete.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View view) { + // Delete white/black list entry + WhitelistAdapter.this.remove(name); + mMapWhitelists.get(mSelectedType).remove(name); + PrivacyManager.setSetting(mUid, mSelectedType, name, null); + if (!wnomod) + PrivacyManager.updateState(mUid); + } + }); + + return convertView; + } +} diff --git a/src/biz/bokhorst/xprivacy/WhitelistTask.java b/src/biz/bokhorst/xprivacy/WhitelistTask.java new file mode 100644 index 000000000..5e13bd426 --- /dev/null +++ b/src/biz/bokhorst/xprivacy/WhitelistTask.java @@ -0,0 +1,111 @@ +package biz.bokhorst.xprivacy; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; + +import android.annotation.SuppressLint; +import android.app.AlertDialog; +import android.content.Context; +import android.content.DialogInterface; +import android.os.AsyncTask; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; +import android.widget.ListView; +import android.widget.Spinner; +import android.widget.AdapterView.OnItemSelectedListener; + +public class WhitelistTask extends AsyncTask { + private int mUid; + private String mType; + private ActivityBase mContext; + private WhitelistAdapter mWhitelistAdapter; + private Map> mListWhitelist; + + public WhitelistTask(int uid, String type, ActivityBase context) { + mUid = uid; + mType = type; + mContext = context; + } + + @Override + protected Object doInBackground(Object... params) { + mListWhitelist = PrivacyManager.listWhitelisted(mUid, null); + mWhitelistAdapter = new WhitelistAdapter(mContext, R.layout.whitelistentry, mUid, mListWhitelist); + return null; + } + + @Override + @SuppressLint({ "DefaultLocale", "InflateParams" }) + protected void onPostExecute(Object result) { + if (!mContext.isFinishing()) { + // Build dialog + AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext); + alertDialogBuilder.setTitle(R.string.menu_whitelists); + alertDialogBuilder.setIcon(mContext.getThemed(R.attr.icon_launcher)); + + if (mListWhitelist.keySet().size() > 0) { + LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + View llWhitelists = inflater.inflate(R.layout.whitelists, null); + + int index = 0; + int selected = -1; + final List localizedType = new ArrayList(); + for (String type : mListWhitelist.keySet()) { + String name = "whitelist_" + type.toLowerCase().replace("/", ""); + int id = mContext.getResources().getIdentifier(name, "string", mContext.getPackageName()); + if (id == 0) + localizedType.add(name); + else + localizedType.add(mContext.getResources().getString(id)); + + if (type.equals(mType)) + selected = index; + index++; + } + + Spinner spWhitelistType = (Spinner) llWhitelists.findViewById(R.id.spWhitelistType); + ArrayAdapter whitelistTypeAdapter = new ArrayAdapter(mContext, + android.R.layout.simple_spinner_dropdown_item, localizedType); + spWhitelistType.setAdapter(whitelistTypeAdapter); + spWhitelistType.setOnItemSelectedListener(new OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView parent, View view, int position, long id) { + mWhitelistAdapter.setType(mListWhitelist.keySet().toArray(new String[0])[position]); + } + + @Override + public void onNothingSelected(AdapterView view) { + } + }); + if (selected >= 0) + spWhitelistType.setSelection(selected); + + ListView lvWhitelist = (ListView) llWhitelists.findViewById(R.id.lvWhitelist); + lvWhitelist.setAdapter(mWhitelistAdapter); + int position = spWhitelistType.getSelectedItemPosition(); + if (position != AdapterView.INVALID_POSITION) + mWhitelistAdapter.setType(mListWhitelist.keySet().toArray(new String[0])[position]); + + alertDialogBuilder.setView(llWhitelists); + } + + alertDialogBuilder.setPositiveButton(mContext.getString(R.string.msg_done), + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + // Do nothing + } + }); + + // Show dialog + AlertDialog alertDialog = alertDialogBuilder.create(); + alertDialog.show(); + } + + super.onPostExecute(result); + } +}