src/com/beem/project/beem/ui/GroupList.java
changeset 447 2771e1b2e080
parent 443 97c8c32b61d0
child 448 aa654c8efd1b
equal deleted inserted replaced
446:b33eed682a0d 447:2771e1b2e080
    12 import android.os.RemoteException;
    12 import android.os.RemoteException;
    13 import android.util.Log;
    13 import android.util.Log;
    14 import android.view.KeyEvent;
    14 import android.view.KeyEvent;
    15 import android.view.View;
    15 import android.view.View;
    16 import android.view.View.OnKeyListener;
    16 import android.view.View.OnKeyListener;
       
    17 import android.widget.AdapterView;
    17 import android.widget.ArrayAdapter;
    18 import android.widget.ArrayAdapter;
    18 import android.widget.CheckedTextView;
    19 import android.widget.CheckedTextView;
    19 import android.widget.ListView;
    20 import android.widget.ListView;
    20 import android.widget.TextView;
    21 import android.widget.TextView;
       
    22 import android.widget.AdapterView.OnItemClickListener;
    21 
    23 
    22 import com.beem.project.beem.BeemService;
    24 import com.beem.project.beem.BeemService;
    23 import com.beem.project.beem.R;
    25 import com.beem.project.beem.R;
    24 import com.beem.project.beem.service.Contact;
    26 import com.beem.project.beem.service.Contact;
    25 import com.beem.project.beem.service.aidl.IRoster;
    27 import com.beem.project.beem.service.aidl.IRoster;
    63 	mJID = mContact.getJID();
    65 	mJID = mContact.getJID();
    64 	final ListView listView = getListView();
    66 	final ListView listView = getListView();
    65 
    67 
    66 	listView.setItemsCanFocus(false);
    68 	listView.setItemsCanFocus(false);
    67 	listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    69 	listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
       
    70 	listView.setOnItemClickListener(new GroupOnItemClickListener());
    68 
    71 
    69 	mText = (TextView) findViewById(R.id.GroupListText);
    72 	mText = (TextView) findViewById(R.id.GroupListText);
    70 	mText.setOnKeyListener(new GroupListOnKeyListener());
    73 	mText.setOnKeyListener(new GroupListOnKeyListener());
    71     }
    74     }
    72 
    75 
    98      * @param v The view that was clicked within the ListView
   101      * @param v The view that was clicked within the ListView
    99      * @param position The position of the view in the list
   102      * @param position The position of the view in the list
   100      * @param id The row id of the item that was clicked
   103      * @param id The row id of the item that was clicked
   101      */
   104      */
   102     protected void onListItemClick(ListView l, View v, int position, long id) {
   105     protected void onListItemClick(ListView l, View v, int position, long id) {
   103 	Log.d("GROUPLIST", "CLICK");
   106 
   104 	CheckedTextView textView = (CheckedTextView) v;
       
   105 	if (textView.isChecked()) {	    
       
   106 	    try {
       
   107 		mRoster.addContactToGroup(textView.getText().toString(), mJID);
       
   108 	    } catch (RemoteException e) {
       
   109 		e.printStackTrace();
       
   110 	    }
       
   111 	} else {
       
   112 	    try {
       
   113 		mRoster.removeContactFromGroup(textView.getText().toString(), mJID);
       
   114 	    } catch (RemoteException e) {
       
   115 		e.printStackTrace();
       
   116 	    }
       
   117 	}
       
   118     }
   107     }
   119 
   108 
   120     /**
   109     /**
   121      * init activity lsit adapter.
   110      * init activity lsit adapter.
   122      */
   111      */
   123     private void setAdapter() {
   112     private void setAdapter() {
   124 	try {
   113 	try {
   125 	    for (String group: mRoster.getGroupsNames()) {
   114 	    for (String group : mRoster.getGroupsNames()) {
   126 		mStrings.add(group);
   115 		mStrings.add(group);
   127 	    }
   116 	    }
   128 	    mGroups = new ArrayAdapter<String>(this,
   117 	    mGroups = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, mStrings);
   129 		android.R.layout.simple_list_item_multiple_choice, mStrings);
       
   130 	    setListAdapter(mGroups);
   118 	    setListAdapter(mGroups);
   131 	    mContact = mRoster.getContact(mJID);
   119 	    mContact = mRoster.getContact(mJID);
   132 	    for (String group : mContact.getGroups()) {
   120 	    for (String group : mContact.getGroups()) {
   133 		getListView().setItemChecked(mGroups.getPosition(group), true);
   121 		getListView().setItemChecked(mGroups.getPosition(group), true);
   134 	    }
   122 	    }
   148 			if (mText.getText().length() == 0)
   136 			if (mText.getText().length() == 0)
   149 			    return false;
   137 			    return false;
   150 			String groupname = mText.getText().toString();
   138 			String groupname = mText.getText().toString();
   151 			mGroups.add(groupname);
   139 			mGroups.add(groupname);
   152 			mText.setText(null);
   140 			mText.setText(null);
   153 			try {
       
   154 			    mRoster.createGroup(groupname);
       
   155 			} catch (RemoteException e) {
       
   156 			    e.printStackTrace();
       
   157 			}
       
   158 			return true;
   141 			return true;
   159 		    default:
   142 		    default:
   160 			return false;
   143 			return false;
   161 		}
   144 		}
   162 	    }
   145 	    }
   163 	    return false;
   146 	    return false;
   164 	}
   147 	}
   165 
   148 
   166     }
   149     }
   167 
   150 
       
   151     private class GroupOnItemClickListener implements OnItemClickListener {
       
   152 
       
   153 	@Override
       
   154 	public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3) {
       
   155 	    Log.d("GROUPLIST", "CLICK");
       
   156 	    CheckedTextView textView = (CheckedTextView) v;
       
   157 	    if (!textView.isChecked()) {
       
   158 		try {
       
   159 		    mRoster.createGroup(textView.getText().toString());
       
   160 		    mRoster.addContactToGroup(textView.getText().toString(), mJID);
       
   161 		} catch (RemoteException e) {
       
   162 		    e.printStackTrace();
       
   163 		}
       
   164 	    } else {
       
   165 		try {
       
   166 		    mRoster.removeContactFromGroup(textView.getText().toString(), mJID);
       
   167 		} catch (RemoteException e) {
       
   168 		    e.printStackTrace();
       
   169 		}
       
   170 	    }
       
   171 
       
   172 	}
       
   173 
       
   174     }
       
   175 
   168     /**
   176     /**
   169      * The ServiceConnection used to connect to the Beem service.
   177      * The ServiceConnection used to connect to the Beem service.
   170      */
   178      */
   171     private class BeemServiceConnection implements ServiceConnection {
   179     private class BeemServiceConnection implements ServiceConnection {
   172 
   180 
   173 	/**
   181 	/**
   174 	 * Constructor.
   182 	 * Constructor.
   175 	 */
   183 	 */
   176 	public BeemServiceConnection() { }
   184 	public BeemServiceConnection() {
       
   185 	}
   177 
   186 
   178 	@Override
   187 	@Override
   179 	public void onServiceConnected(ComponentName name, IBinder service) {
   188 	public void onServiceConnected(ComponentName name, IBinder service) {
   180 	    mXmppFacade = IXmppFacade.Stub.asInterface(service);
   189 	    mXmppFacade = IXmppFacade.Stub.asInterface(service);
   181 	    try {
   190 	    try {