Last active
December 27, 2015 05:09
-
-
Save rxco/7271951 to your computer and use it in GitHub Desktop.
Android Background Jobs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivs 3.0 | |
Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/3.0/ | |
or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.*/ | |
package com.leyendalabs.guarurapp.framents; | |
import java.util.ArrayList; | |
import java.util.List; | |
import android.content.Intent; | |
import android.database.Cursor; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.net.Uri; | |
import android.os.AsyncTask; | |
import android.os.Bundle; | |
import android.provider.ContactsContract; | |
import android.provider.ContactsContract.Contacts; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.AdapterView; | |
import android.widget.AdapterView.OnItemClickListener; | |
import android.widget.ListView; | |
import android.widget.Toast; | |
import com.actionbarsherlock.app.SherlockFragment; | |
import com.actionbarsherlock.app.SherlockFragmentActivity; | |
import com.actionbarsherlock.view.Menu; | |
import com.actionbarsherlock.view.MenuInflater; | |
import com.actionbarsherlock.view.MenuItem; | |
import com.leyendalabs.guarurapp.R; | |
import com.leyendalabs.guarurapp.Selecciona_Contactos; | |
import com.leyendalabs.guarurapp.base_de_datos.ContactosDataSource; | |
import com.leyendalabs.guarurapp.base_de_datos.ContactosNotificacionBean; | |
import com.leyendalabs.guarurapp.especiales.Contacto; | |
import com.leyendalabs.guarurapp.especiales.My_ListView_Contacts; | |
public class Fragment_Contactos extends SherlockFragment implements | |
OnItemClickListener { | |
private ViewGroup viewGroup; | |
private final int ID_MENU_1 = 1; | |
private final int ID_MENU_2 = 2; | |
private int CODE = 10; | |
private ListView lista_mostrar; | |
private My_ListView_Contacts listView_Contacts; | |
private ArrayList<Contacto> List_Contac; | |
private ContactosDataSource dataSource; | |
int indicador = 0; | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
viewGroup = (ViewGroup) inflater.inflate(R.layout.contactos, null, | |
false); | |
setHasOptionsMenu(true); | |
return viewGroup; | |
} | |
@Override | |
public void onActivityCreated(Bundle savedInstanceState) { | |
// TODO Auto-generated method stub | |
super.onActivityCreated(savedInstanceState); | |
dataSource = new ContactosDataSource(getSherlockActivity()); | |
lista_mostrar = (ListView) viewGroup.findViewById(R.id.listaconta); | |
new Hilo_Muestras(getSherlockActivity()).execute(); | |
} | |
@Override | |
public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
// TODO Auto-generated method stub | |
if (requestCode == CODE) { | |
if (resultCode == SherlockFragmentActivity.RESULT_OK) { | |
Toast.makeText(getSherlockActivity(), "Nuevos Contactos", | |
Toast.LENGTH_SHORT).show(); | |
// dataSource = new ContactosDataSource(getSherlockActivity()); | |
// | |
// lista_mostrar = (ListView) viewGroup | |
// .findViewById(R.id.listaconta); | |
new Hilo_Muestras(getSherlockActivity()).execute(); | |
} | |
} | |
} | |
@Override | |
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { | |
// TODO Auto-generated method stub | |
super.onCreateOptionsMenu(menu, inflater); | |
menu.clear(); | |
menu.add(0, ID_MENU_2, 0, "Borrar") | |
.setIcon(R.drawable.borrar) | |
.setShowAsAction( | |
MenuItem.SHOW_AS_ACTION_IF_ROOM | |
| MenuItem.SHOW_AS_ACTION_WITH_TEXT); | |
menu.add(0, ID_MENU_1, 0, "AÒadir") | |
.setIcon(R.drawable.anadir) | |
.setShowAsAction( | |
MenuItem.SHOW_AS_ACTION_IF_ROOM | |
| MenuItem.SHOW_AS_ACTION_WITH_TEXT); | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
switch (item.getItemId()) { | |
case ID_MENU_1: | |
Intent intent = new Intent(getSherlockActivity(), | |
Selecciona_Contactos.class); | |
startActivityForResult(intent, CODE); | |
break; | |
case ID_MENU_2: | |
if (indicador > 0) { | |
borrar(); | |
} else { | |
Toast.makeText(getSherlockActivity(), | |
"Seleccione un elemento para borrar ", | |
Toast.LENGTH_SHORT).show(); | |
} | |
break; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
private void crear_lista() { | |
listView_Contacts = new My_ListView_Contacts(getSherlockActivity(), | |
List_Contac); | |
lista_mostrar.setAdapter(listView_Contacts); | |
lista_mostrar.setOnItemClickListener(this); | |
} | |
@Override | |
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { | |
if (List_Contac.get(arg2).getSeleccionado()) { | |
List_Contac.get(arg2).setSeleccionado(false); | |
indicador--; | |
} else { | |
List_Contac.get(arg2).setSeleccionado(true); | |
indicador++; | |
} | |
listView_Contacts.notifyDataSetChanged(); | |
} | |
private void borrar() { | |
int i = 0; | |
Contacto contacto; | |
do { | |
contacto = List_Contac.get(i); | |
if (contacto.getSeleccionado()) { | |
dataSource.borrarContacto(contacto.getlookup_key()); | |
List_Contac.remove(i); | |
} else { | |
i++; | |
} | |
} while (i < List_Contac.size()); | |
listView_Contacts.notifyDataSetChanged(); | |
} | |
private class Hilo_Muestras extends AsyncTask<Void, Void, Boolean> { | |
private SherlockFragmentActivity contexto; | |
private List<ContactosNotificacionBean> arrayListBase; | |
public Hilo_Muestras(SherlockFragmentActivity contexto) { | |
// TODO Auto-generated constructor stub | |
this.contexto = contexto; | |
List_Contac = new ArrayList<Contacto>(); | |
} | |
@Override | |
protected Boolean doInBackground(Void... params) { | |
arrayListBase = dataSource.obtenerContactos(); | |
if (arrayListBase.size() == 0) { | |
return false; | |
} | |
Uri contactsUri = ContactsContract.Contacts.CONTENT_URI; | |
Uri dataUri = ContactsContract.Data.CONTENT_URI; | |
Cursor contactsCursor; | |
Cursor dataCursor; | |
String nombre = ""; | |
byte[] fotoByte = null; | |
Bitmap bitmap = null; | |
for (int i = 0; i < arrayListBase.size(); i++) { | |
Log.d("String", arrayListBase.get(i).getIdContacto()); | |
contactsCursor = contexto.getContentResolver().query( | |
contactsUri, null, Contacts.LOOKUP_KEY + "=?", | |
new String[] { arrayListBase.get(i).getIdContacto() }, | |
null); | |
if (contactsCursor.moveToFirst()) { | |
long contactId = contactsCursor.getLong(contactsCursor | |
.getColumnIndex(Contacts._ID)); | |
dataCursor = contexto.getContentResolver().query(dataUri, | |
null, ContactsContract.Data.CONTACT_ID + "=?", | |
new String[] { String.valueOf(contactId) }, null); | |
if (dataCursor.moveToFirst()) { | |
nombre = dataCursor | |
.getString(dataCursor | |
.getColumnIndex(ContactsContract.Data.DISPLAY_NAME)); | |
do { | |
if (dataCursor | |
.getString( | |
dataCursor | |
.getColumnIndex("mimetype")) | |
.equals(ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)) { | |
fotoByte = dataCursor.getBlob(dataCursor | |
.getColumnIndex("data15")); | |
if (fotoByte != null) { | |
bitmap = BitmapFactory.decodeByteArray( | |
fotoByte, 0, fotoByte.length); | |
} | |
break; | |
} | |
} while (dataCursor.moveToNext()); | |
} | |
dataCursor.close(); | |
} | |
List_Contac.add(new Contacto(arrayListBase.get(i) | |
.getIdContacto(), nombre, arrayListBase.get(i) | |
.getNumero(), bitmap, false)); | |
nombre = ""; | |
bitmap = null; | |
contactsCursor.close(); | |
} | |
return true; | |
} | |
@Override | |
protected void onPostExecute(Boolean result) { | |
// TODO Auto-generated method stub | |
super.onPostExecute(result); | |
if (result) { | |
crear_lista(); | |
} else { | |
Toast.makeText(contexto, "No hay elemento que mostrar!!!", | |
Toast.LENGTH_SHORT).show(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment