178 lines
5.8 KiB
Java
178 lines
5.8 KiB
Java
package beyondsoft.bewomitarbeiterapp;
|
|
|
|
import android.app.ListActivity;
|
|
import android.content.Intent;
|
|
import android.graphics.Bitmap;
|
|
import android.graphics.BitmapFactory;
|
|
import android.os.Bundle;
|
|
import android.util.Log;
|
|
import android.view.Menu;
|
|
import android.view.MenuItem;
|
|
import android.view.View;
|
|
import android.widget.ListView;
|
|
import android.widget.Toast;
|
|
|
|
import com.google.gson.Gson;
|
|
import com.google.gson.GsonBuilder;
|
|
import com.google.gson.reflect.TypeToken;
|
|
import com.octo.android.robospice.SpiceManager;
|
|
import com.octo.android.robospice.UncachedSpiceService;
|
|
import com.octo.android.robospice.persistence.exception.SpiceException;
|
|
import com.octo.android.robospice.request.listener.RequestListener;
|
|
|
|
import org.ksoap2.serialization.PropertyInfo;
|
|
import org.ksoap2.serialization.SoapPrimitive;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.lang.reflect.Type;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import entities.ChatPerson;
|
|
import soapConnection.JsonSoapPrimitiveRequest;
|
|
import soapConnection.SoapCalls;
|
|
import soapConnection.SoapConnectionManager;
|
|
|
|
/**
|
|
* Created by bib on 02.05.2016.
|
|
*/
|
|
public class KontaktChatActivity extends ListActivity {
|
|
|
|
private SpiceManager spiceManager = new SpiceManager(UncachedSpiceService.class);
|
|
private DatabaseHandler databaseHandler;
|
|
|
|
private ArrayList<Integer> onlinePersonOids = new ArrayList<>();
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
|
|
databaseHandler = new DatabaseHandler(this);
|
|
|
|
//TODO: nicht löschen, sondern updaten...
|
|
//databaseHandler.onUpgrade(databaseHandler.getReadableDatabase(), 1, 2);
|
|
|
|
Bundle bundle = getIntent().getExtras();
|
|
onlinePersonOids = bundle.getIntegerArrayList("OnlineContacts");
|
|
|
|
ArrayList<PropertyInfo> propertyInfos = new ArrayList<>();
|
|
propertyInfos.add(SoapConnectionManager.BuildProperty("pPersonOid", SoapConnectionManager.User.getPersonOid(), Long.class));
|
|
|
|
JsonSoapPrimitiveRequest request = new JsonSoapPrimitiveRequest(SoapCalls.GET_CONTACTS_FOR_PERSON, propertyInfos);
|
|
spiceManager.execute(request, new JsonSoapPrimitiveRequestListener());
|
|
}
|
|
|
|
@Override
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
// Inflate the menu; this adds items to the action bar if it is present.
|
|
getMenuInflater().inflate(R.menu.menu_main_activity_mitarbeiter, menu);
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
// Handle action bar item clicks here. The action bar will
|
|
// automatically handle clicks on the Home/Up button, so long
|
|
// as you specify a parent activity in AndroidManifest.xml.
|
|
int id = item.getItemId();
|
|
|
|
//noinspection SimplifiableIfStatement
|
|
|
|
if (id == R.id.action_Logout) {
|
|
System.exit(0);
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
}
|
|
|
|
@Override
|
|
protected void onListItemClick(ListView l, View v, int position, long id) {
|
|
//get selected items
|
|
ContactListItem selectedValue = (ContactListItem) getListAdapter().getItem(position);
|
|
|
|
Bundle bundle = new Bundle();
|
|
|
|
bundle.putString("Name", selectedValue.getChatName());
|
|
bundle.putInt("RecipientOid", selectedValue.getPersonOid());
|
|
|
|
Intent in = new Intent(getBaseContext(), ChatActivity.class);
|
|
in.putExtras(bundle);
|
|
|
|
//Gib Bild für den Chat mit
|
|
if(selectedValue.getPicture() == null) {
|
|
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
|
Bitmap a = BitmapFactory.decodeResource(getResources(), R.drawable.katze3);
|
|
a.compress(Bitmap.CompressFormat.JPEG,0,stream);
|
|
byte[] x = stream.toByteArray();
|
|
in.putExtra("Bild", x);
|
|
} else {
|
|
in.putExtra("Bild", selectedValue.getPicture());
|
|
}
|
|
|
|
startActivity(in);
|
|
}
|
|
|
|
@Override
|
|
public void onStop(){
|
|
spiceManager.shouldStop();
|
|
super.onStop();
|
|
}
|
|
|
|
@Override
|
|
protected void onStart() {
|
|
super.onStart();
|
|
spiceManager.start(this);
|
|
}
|
|
|
|
private final class JsonSoapPrimitiveRequestListener implements RequestListener<SoapPrimitive> {
|
|
|
|
@Override
|
|
public void onRequestFailure(SpiceException spiceException) {
|
|
Toast.makeText(KontaktChatActivity.this, "An error occured while loading the contact list", Toast.LENGTH_SHORT).show();
|
|
}
|
|
|
|
@Override
|
|
public void onRequestSuccess(SoapPrimitive soapPrimitive) {
|
|
|
|
if(soapPrimitive == null) {
|
|
Log.i("KONTAKT_CHAT_ACTIVITY", "soapPrimitive ist NULL!");
|
|
}
|
|
|
|
Log.i("SoapPrmitive", "" + soapPrimitive.toString());
|
|
|
|
Gson gson = new GsonBuilder().create();
|
|
|
|
Type listType = new TypeToken<ArrayList<ChatPerson>>(){}.getType();
|
|
|
|
ArrayList<ChatPerson> result = gson.fromJson(soapPrimitive.toString(), listType);
|
|
|
|
List<ContactListItem> lc = databaseHandler.getAllContacts();
|
|
|
|
List<Integer> oidList = new ArrayList<>();
|
|
|
|
for(ContactListItem item : lc) {
|
|
oidList.add(item.getPersonOid());
|
|
}
|
|
|
|
for(ChatPerson cp : result) {
|
|
if(!oidList.contains(cp.Oid)) {
|
|
databaseHandler.addContact(new ContactListItem(cp.Name, cp.Oid));
|
|
}
|
|
}
|
|
|
|
lc = databaseHandler.getAllContacts();
|
|
|
|
for(ContactListItem item : lc) {
|
|
if(onlinePersonOids.contains(item.getPersonOid())){
|
|
item.isOnline = true;
|
|
}
|
|
}
|
|
|
|
ContactListItem[] Klientel = lc.toArray(new ContactListItem[lc.size()]);
|
|
|
|
setListAdapter(new KontaktAdapter(KontaktChatActivity.this, Klientel));
|
|
databaseHandler.close();
|
|
}
|
|
}
|
|
}
|