Android Open Source - wiki-contacts-android Search Activity






From Project

Back to project page wiki-contacts-android.

License

The source code is released under:

Apache License

If you think the Android project wiki-contacts-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.kahkong.wikicontacts.controller;
/*w  w  w .ja v  a 2 s.  co  m*/
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.kahkong.wikicontacts.R;
import com.kahkong.wikicontacts.callback.OnCompleteListener;
import com.kahkong.wikicontacts.callback.OnEventListener;
import com.kahkong.wikicontacts.form.OptionsForm;
import com.kahkong.wikicontacts.modal.Contact;
import com.kahkong.wikicontacts.modal.ContactAndImage;
import com.kahkong.wikicontacts.service.ContactService;
import com.kahkong.wikicontacts.service.ContactServiceImpl;
import com.kahkong.wikicontacts.service.ImageService;
import com.kahkong.wikicontacts.service.ImageServiceImpl;
import com.kahkong.wikicontacts.service.IntentService;
import com.kahkong.wikicontacts.service.IntentServiceImpl;
import com.kahkong.wikicontacts.service.NotificationService;
import com.kahkong.wikicontacts.service.NotificationServiceImpl;
import com.kahkong.wikicontacts.service.PreferencesService;
import com.kahkong.wikicontacts.service.PreferencesServiceImpl;
import com.kahkong.wikicontacts.service.ResourceService;
import com.kahkong.wikicontacts.service.ResourceServiceImpl;
import com.kahkong.wikicontacts.service.WebService;
import com.kahkong.wikicontacts.service.WebServiceImpl;
import com.kahkong.wikicontacts.subcontroller.InfoDialog;
import com.kahkong.wikicontacts.subcontroller.ViewContactDialog;
import com.kahkong.wikicontacts.subcontroller.ResultAdaptor;

import android.app.Activity;
import android.app.PendingIntent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Parcelable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.view.View.OnClickListener;

/**
 * 
 * @author Poh Kah Kong
 *
 */
public class SearchActivity extends Activity {  
  private ContactService contactService = ContactServiceImpl.getInstance();
  private ResourceService resourceService = ResourceServiceImpl.getInstance();
  private WebService webService = WebServiceImpl.getInstance();
  private ImageService imageService = ImageServiceImpl.getInstance();
  private PreferencesService preferencesService = PreferencesServiceImpl.getInstance();
  private IntentService intentService = IntentServiceImpl.getInstance();
  private NotificationService notificationService = NotificationServiceImpl.getInstance();

  private View backgroundView;
  private EditText inputET;
  private ProgressBar loadingPB;
  private ListView resultLV;
  private Button addBtn;
  private ImageView resetIV;
  private ImageView optionsIV;
  private ResultAdaptor resultAdaptor;
  private OptionsForm optionsOF;
  private String query;  

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.application);
    resourceService.setContext(this);
    preferencesService.setSharedPreferences(this);
    intentService.setActivity(this);
    notificationService.setNotificationManager(this);
    
    FrameLayout applicationFL = (FrameLayout) findViewById(R.id.applicationFL);
    backgroundView = getLayoutInflater().inflate(R.layout.background, null);
    View searchView = getLayoutInflater().inflate(R.layout.search, null);
    applicationFL.addView(backgroundView);
    applicationFL.addView(searchView);

    inputET = (EditText) findViewById(R.id.searchInputET);
    loadingPB = (ProgressBar) findViewById(R.id.searchLoadingPB);
    resultLV = (ListView) findViewById(R.id.searchResultLV);
    addBtn = (Button) findViewById(R.id.searchAddBtn);
    resetIV = (ImageView) findViewById(R.id.searchResetIV);
    optionsIV = (ImageView) findViewById(R.id.searchOptionsIV);
    optionsOF = (OptionsForm) findViewById(R.id.searchOptionsOF);
    
    initResultAdaptor();
    initSearchET();
    initResultLV();
    initBtns();
    initOptionsOP();
    initPreferences();
  }
  
  private void initResultAdaptor() {
    resultAdaptor = new ResultAdaptor(SearchActivity.this, null);
    resultLV.setAdapter(resultAdaptor);  
  }
  
  private void initSearchET() {
    inputET.addTextChangedListener(new TextWatcher() {
      @Override
      public void afterTextChanged(Editable editable) {
        query = editable.toString();
        if (query.equals("")) {
          webService.appCancel();
          resultAdaptor.clear();  
          loadingPB.setVisibility(View.INVISIBLE);
          resetIV.setVisibility(View.INVISIBLE);
          backgroundView.setVisibility(View.VISIBLE);
          return;
        }
        search();
      }

      @Override
      public void beforeTextChanged(CharSequence s, int start, int count, int after) {
      }

      @Override
      public void onTextChanged(CharSequence s, int start, int before, int count) {              
      }
    });
  }
  
  private void initResultLV() {
    resultLV.setOnItemClickListener(new OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        final ContactAndImage contactAndImage = (ContactAndImage) resultAdaptor.getItem(position);
        ViewContactDialog viewContactDialog = new ViewContactDialog(SearchActivity.this, contactAndImage, new OnEventListener() {
          @Override
          public void onEvent(Object event, Object data) {
            switch ((ViewContactDialog.Event) event) {
            case EDIT_CLICK:
              Bitmap image = null;
              if (contactAndImage.getImageUrl()!=null) {
                image = contactAndImage.getImage();
              }              
              String[] name = {"Contact", "Image"};
              Parcelable [] extra = {(Contact) contactAndImage, image};
              intentService.startActivityWithParcelable(UpdateEditContactActivity.class, name, extra);              
              break;
            case TAG_CLICK:
              if (!query.equals("")) {
                inputET.setText(query + " "  + (String) data);
              } else {
                inputET.setText((String) data);
              }              
              break;
            default:
              break;            
            }
                        
          }          
        });
        viewContactDialog.show();
      }      
    });
  }
  
  private void initBtns() {
    addBtn.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View view) {
        intentService.startActivity(AddEditContactActivity.class);            
      }      
    });
    
    resetIV.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View view) {
        resetSearch();
      }      
    });
    
    optionsIV.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View view) {
        optionsOF.setVisibility(View.VISIBLE);
      }      
    });
  }
  
  private void initOptionsOP() {
    optionsOF.setCallId(preferencesService.getCallerId());
    optionsOF.setVisibility(View.INVISIBLE);
    optionsOF.setOnEventListener(new OnEventListener() {
      @Override
      public void onEvent(Object event, Object data) {
        switch ((OptionsForm.Event) event) {
        case CALLID_ON:
          startCallDetectService();
          break;
        case CALLID_OFF:
          stopCallDetectService();
          break;
        case HOMEPAGE:
          intentService.startUrlActivity(getResources().getString(R.string.app_web));
          break;          
        case FEEDBACK:
          intentService.startEmailActivity(getResources().getString(R.string.app_email), getResources().getString(R.string.app_name));
          break;
        case RATE:
          intentService.startUrlActivity(getResources().getString(R.string.app_android));
          break;          
        case ICONS:
          intentService.startUrlActivity(getResources().getString(R.string.search_iconsurl));
          break;          
        default:
          break;        
        }
      }      
    });
  }
  
  private void initPreferences() {
    if (preferencesService.getCallerId()) {
      startCallDetectService();
    } else {
      stopCallDetectService();
    }
    if (!preferencesService.getInfoHidden()) {
      InfoDialog infoDialog = new InfoDialog(this);
      infoDialog.show();
    }
  }
  
  private List<ContactAndImage> contactsToViewContacts(List<Contact> contacts) {
    if (contacts==null) {
      return null;
    }    
    List<ContactAndImage> viewContacts = new ArrayList<ContactAndImage>();
    Iterator <Contact> iterator = contacts.iterator();
    while (iterator.hasNext()) {
      final ContactAndImage contactAndImage = new ContactAndImage(iterator.next()); 
      viewContacts.add(contactAndImage);      
      imageService.getImage(contactAndImage.isSpam(), contactAndImage.getImageUrl(), new OnCompleteListener() {
        @Override
        public void onComplete(Object[] objects) {  
          Bitmap image = (Bitmap) objects[0];
          contactAndImage.setImage(image);
          resultAdaptor.notifyDataSetChanged();
        }          
      });  
    }
    return viewContacts;
  }
  
  private void search() {  
    if (query==null || query.equals("") || query.matches(".*\\s+$")) {
      return;
    }
    
    loadingPB.setVisibility(View.VISIBLE);
    resetIV.setVisibility(View.INVISIBLE);
    backgroundView.setVisibility(View.INVISIBLE);
    contactService.getContacts(query, new OnCompleteListener() {
      @SuppressWarnings("unchecked")
      @Override
      public void onComplete(Object[] objects) {        
        List<Contact> contacts = (List<Contact>) objects[0];
        if (contacts==null) { // contact not in database
          loadingPB.setVisibility(View.INVISIBLE);
          return;
        }
        resultAdaptor.setContacts(contactsToViewContacts(contacts));
        resultAdaptor.notifyDataSetChanged();
        loadingPB.setVisibility(View.INVISIBLE);
        resetIV.setVisibility(View.VISIBLE);
      }      
    });    
  }
  
  private void resetSearch() {
    inputET.setText("");
  }
  
  private void startCallDetectService() {
    intentService.startService(CallDetectService.class);
    preferencesService.setCallerId(true);
    PendingIntent pendingIntent = intentService.getPendingIntent(SearchActivity.class, notificationService.getRequestCode());
    notificationService.show(getResources().getString(R.string.notification_title), getResources().getString(R.string.notification_content), pendingIntent);    
  }
  
  private void stopCallDetectService() {
    intentService.stopService(CallDetectService.class);  
    preferencesService.setCallerId(false);
    notificationService.cancel();    
  }  
  
  @Override
  protected void onPause() {
    webService.appCancel();
    super.onPause();
  }

  @Override
  protected void onResume() {
    super.onResume();
    search();
  }

  @Override
  public void onBackPressed() {
    if (optionsOF.getVisibility()==View.VISIBLE) {
      optionsOF.setVisibility(View.GONE);
      return;
    }
    super.onBackPressed();
  }
}




Java Source Code List

com.kahkong.wikicontacts.callback.OnChangeListener.java
com.kahkong.wikicontacts.callback.OnClickListener.java
com.kahkong.wikicontacts.callback.OnCompleteListener.java
com.kahkong.wikicontacts.callback.OnEventListener.java
com.kahkong.wikicontacts.callback.OnTimeOutListener.java
com.kahkong.wikicontacts.callback.Validator.java
com.kahkong.wikicontacts.controller.AddEditContactActivity.java
com.kahkong.wikicontacts.controller.CallDetectService.java
com.kahkong.wikicontacts.controller.EditContactActivity.java
com.kahkong.wikicontacts.controller.SearchActivity.java
com.kahkong.wikicontacts.controller.TestActivity.java
com.kahkong.wikicontacts.controller.UpdateEditContactActivity.java
com.kahkong.wikicontacts.form.EditTextForm.java
com.kahkong.wikicontacts.form.ImageUploadForm.java
com.kahkong.wikicontacts.form.MultiEditTextForm.java
com.kahkong.wikicontacts.form.OptionsForm.java
com.kahkong.wikicontacts.form.RadioGroupForm.java
com.kahkong.wikicontacts.form.SpinnerForm.java
com.kahkong.wikicontacts.form.TagsForm.java
com.kahkong.wikicontacts.link.EmailLink.java
com.kahkong.wikicontacts.link.Link.java
com.kahkong.wikicontacts.link.NumberLink.java
com.kahkong.wikicontacts.link.TagLink.java
com.kahkong.wikicontacts.link.UrlLink.java
com.kahkong.wikicontacts.modal.ContactAndImage.java
com.kahkong.wikicontacts.modal.Contact.java
com.kahkong.wikicontacts.modal.CountryCode.java
com.kahkong.wikicontacts.modal.CountryCodesWithSelect.java
com.kahkong.wikicontacts.service.ContactServiceImpl.java
com.kahkong.wikicontacts.service.ContactService.java
com.kahkong.wikicontacts.service.CountryCodeServiceImpl.java
com.kahkong.wikicontacts.service.CountryCodeService.java
com.kahkong.wikicontacts.service.ImageServiceImpl.java
com.kahkong.wikicontacts.service.ImageService.java
com.kahkong.wikicontacts.service.IntentServiceImpl.java
com.kahkong.wikicontacts.service.IntentService.java
com.kahkong.wikicontacts.service.NotificationServiceImpl.java
com.kahkong.wikicontacts.service.NotificationService.java
com.kahkong.wikicontacts.service.PhoneBookServiceImpl.java
com.kahkong.wikicontacts.service.PhoneBookService.java
com.kahkong.wikicontacts.service.PreferencesServiceImpl.java
com.kahkong.wikicontacts.service.PreferencesService.java
com.kahkong.wikicontacts.service.ResourceServiceImpl.java
com.kahkong.wikicontacts.service.ResourceService.java
com.kahkong.wikicontacts.service.ValidationServiceImpl.java
com.kahkong.wikicontacts.service.ValidationService.java
com.kahkong.wikicontacts.service.WebServiceImpl.java
com.kahkong.wikicontacts.service.WebService.java
com.kahkong.wikicontacts.subcontroller.CallStateListener.java
com.kahkong.wikicontacts.subcontroller.FlowLayout.java
com.kahkong.wikicontacts.subcontroller.IncomingContactLayout.java
com.kahkong.wikicontacts.subcontroller.InfoDialog.java
com.kahkong.wikicontacts.subcontroller.LoadingDialog.java
com.kahkong.wikicontacts.subcontroller.ResultAdaptor.java
com.kahkong.wikicontacts.subcontroller.ViewContactDialog.java
com.kahkong.wikicontacts.subcontroller.ViewFormatter.java