Android Open Source - CallNotifier Main Activity






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;
//w  w  w. j a va  2  s . c om
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.SwitchCompat;
import android.util.TypedValue;
import android.view.*;
import android.widget.*;

import java.util.List;


public class MainActivity extends ActionBarActivity {

    public static final String ENABLED_KEY = "enb_k";
    public static final String TIMEOUT_INDEX_KEY = "time_k";
    public static final String PREF_NAME = "preferences_private";
    private static final int CONTACT_REQ_CODE = 123;

    private SwitchCompat sw_enabled;
    private Button btn_add;
    private Spinner sp_timeout;
    private LinearLayout lay_contacts;
    private IObsController obsController;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        obsController = new ObsController(getApplicationContext());
        linkUI();
        setData();
        setActions();

    }


    @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, 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_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == CONTACT_REQ_CODE && resultCode == RESULT_OK){
            String phoneNo = null,phoneName = null ;
            Uri uri = data.getData();
            Cursor cursor = getContentResolver().query(uri, null, null, null, null);
            cursor.moveToFirst();

            int  phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
            int nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
            phoneNo = cursor.getString(phoneIndex);
            phoneName = cursor.getString(nameIndex);
            addNotifiableContact(phoneName,phoneNo);
        }
    }

    public void linkUI(){
        sw_enabled = (SwitchCompat)findViewById(R.id.sw_enabled);
        sp_timeout = (Spinner)findViewById(R.id.sp_timeout);
        btn_add = (Button)findViewById(R.id.btn_add);
        lay_contacts = (LinearLayout)findViewById(R.id.lay_contacts);
    }

    public void setData(){
        SharedPreferences pref = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
        sw_enabled.setChecked(pref.getBoolean(ENABLED_KEY,false));
        ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
                               R.array.timeout_values, android.R.layout.simple_spinner_item);
        sp_timeout.setAdapter(adapter);
        int index = pref.getInt(TIMEOUT_INDEX_KEY,0);
        sp_timeout.setSelection(index);
        obsController.getObservers(new Runnable() {
            @Override
            public void run() {
                List<Contact> obs = obsController.getObservers();
                for(Contact c:obs){
                    addContactToLayout(c);
                }
            }
        });
    }

    public void setActions(){
        sw_enabled.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SharedPreferences pref = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
                SharedPreferences.Editor editor = pref.edit();
                editor.putBoolean(ENABLED_KEY,isChecked);
                if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD_MR1){
                    editor.apply();
                }else {
                    editor.commit();
                }
            }
        });
        sp_timeout.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                SharedPreferences pref = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
                SharedPreferences.Editor editor = pref.edit();
                int last = pref.getInt(TIMEOUT_INDEX_KEY,0);
                if(last!=position) {
                    editor.putInt(TIMEOUT_INDEX_KEY, position);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD_MR1) {
                        editor.apply();
                    } else {
                        editor.commit();
                    }
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {}
        });
        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
                startActivityForResult(intent, CONTACT_REQ_CODE);
            }
        });
    }



    public void addNotifiableContact (final String name,final String nr){
        if(name== null || nr == null){
            return;
        }
        //show to layout
        obsController.getObservers(new Runnable() {
            @Override
            public void run() {
                boolean exist = false;
                Contact contact = new Contact(name,nr);
                List<Contact> obs = obsController.getObservers();
                if(obs == null){
                    return;
                }
                for(Contact c:obs){
                    if(c.equals(contact)){
                        exist = true;
                        break;
                    }
                }
                if(!exist) {
                    obsController.addObserver(contact);
                    addContactToLayout(contact);
                }
            }
        });

    }

    public void addContactToLayout(final Contact contact){
        TextView txt = new TextView(getApplicationContext(),null,R.style.AppTheme);
        txt.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.contact_text_size));
        txt.setTextColor(getResources().getColor(R.color.abc_primary_text_material_light));
        txt.setText(contact.name + " " + contact.phone);
        FrameLayout container = new FrameLayout(getApplicationContext());
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                                                         ViewGroup.LayoutParams.WRAP_CONTENT);
        container.setLayoutParams(params);
        FrameLayout.LayoutParams txtParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                                                                          ViewGroup.LayoutParams.WRAP_CONTENT);
        txtParams.gravity = Gravity.CENTER_VERTICAL | Gravity.LEFT ;
        txt.setLayoutParams(txtParams);
        Button btn = (Button)getLayoutInflater().inflate(R.layout.btn_del,null);
        FrameLayout.LayoutParams btnParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                                                                          ViewGroup.LayoutParams.WRAP_CONTENT);
        btnParams.gravity = Gravity.CENTER_VERTICAL | Gravity.RIGHT;
        btn.setLayoutParams(btnParams);
        btn.setText(R.string.rem_lbl);

        btn.setTag(R.id.tag_0,contact);
        btn.setTag(R.id.tag_1,container);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Contact c = (Contact)v.getTag(R.id.tag_0);
                obsController.removeObserver(c);
                FrameLayout container = (FrameLayout)v.getTag(R.id.tag_1);
                if(container!=null){
                    lay_contacts.removeView(container);
                }
            }
        });
        container.addView(txt);
        container.addView(btn);
        lay_contacts.addView(container,0);
    }

}




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