Android Open Source - CallNotifier Obs Controller






From Project

Back to project page CallNotifier.

License

The source code is released under:

Apache License

If you think the Android project CallNotifier 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.hevsoft.callnotiffier;
//from   w  w  w .j  ava  2  s  .c o  m
import android.content.Context;
import android.os.AsyncTask;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by elvis.iulian on 11/11/2014.
 */
public class ObsController implements IObsController{


    private Context context;
    private List<Contact> observers;

    public ObsController(Context c){
        context = c;
        restoreObservers(new Runnable() {
            @Override
            public void run() {
                if(observers == null){
                    observers = new ArrayList<Contact>();
                }
                observers = Collections.synchronizedList(observers);
            }
        });
    }

    @Override
    public void addObserver(final Contact c) {
        restoreObservers(new Runnable() {
            @Override
            public void run() {
                if(observers== null){
                    return;
                }
                observers.add(c);
                saveObservers();
            }
        });
    }

    @Override
    public void removeObserver(final Contact c) {
        restoreObservers(new Runnable() {
            @Override
            public void run() {
                if(observers== null){
                    return;
                }
                observers.remove(c);
                saveObservers();
            }
        });
    }

    @Override
    public void notifyObservers(final Subject s) {
        restoreObservers(new Runnable() {
            @Override
            public void run() {
                for(Contact c:observers){
                    SmsWrapper.sendSms(context,context.getResources().getString(R.string.body_pattern),c,s);
                }
            }
        });
    }

    @Override
    public void getObservers(Runnable callback) {
        restoreObservers(callback);
    }

    @Override
    public List<Contact> getObservers() {
        return observers;
    }

    private void saveObservers(){
        new AsyncTask<Void,Void,Void>(){

            @Override
            protected Void doInBackground(Void... params) {
                if(observers == null || context == null){
                    return null;
                }
                File file =context.getFileStreamPath(IObsController.OBS);
                if(file == null){
                    return null;
                }
                try {
                    ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(file));
                    objectOutputStream.writeObject(observers);
                }catch(Exception e){};
                return null;
            }

        }.execute();
    }

    private void restoreObservers(final Runnable callback){
        new AsyncTask<Void,Void,Void>(){

            @Override
            protected Void doInBackground(Void... params) {
                if(context == null ){
                    return null;
                }

                try {
                    FileInputStream stream =context.openFileInput(IObsController.OBS);
                    if(stream == null){
                        return null;
                    }
                    ObjectInputStream objectInputStream = new ObjectInputStream(stream);
                    observers = (List<Contact>)objectInputStream.readObject();
                }catch (Exception e){}
                if(observers == null){
                    observers = new ArrayList<Contact>();
                    observers = Collections.synchronizedList(observers);
                }

                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                if(callback!=null) {
                    callback.run();
                }
            }
        }.execute();
    }
}




Java Source Code List

com.hevsoft.callnotiffier.ApplicationTest.java
com.hevsoft.callnotiffier.CallsReceiver.java
com.hevsoft.callnotiffier.Configuration.java
com.hevsoft.callnotiffier.Contact.java
com.hevsoft.callnotiffier.IObsController.java
com.hevsoft.callnotiffier.MainActivity.java
com.hevsoft.callnotiffier.ObsController.java
com.hevsoft.callnotiffier.SmsWrapper.java
com.hevsoft.callnotiffier.Subject.java