Android Open Source - txttr User Manager






From Project

Back to project page txttr.

License

The source code is released under:

MIT License

If you think the Android project txttr 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.izgoy.txttr;
//from  w ww. j ava  2 s  .  co m
import android.content.Context;
import android.content.SharedPreferences;
import android.telephony.SmsManager;
import android.util.Log;

import java.util.ArrayList;
import java.util.Map;

public class UserManager {

    public static final String TAG = "UserManager";

    private SharedPreferences db;
    private SmsManager smsManager;

    public UserManager(Context context) {
        db = context.getSharedPreferences(TxttrApplication.PREFS_USERS, Context.MODE_PRIVATE);
        smsManager = SmsManager.getDefault();
    }

    public Map<String, String> getAll() {
        return (Map<String, String>) db.getAll();
    }

    public void register(String address, String nick) {
        SharedPreferences.Editor editor = db.edit();
        editor.putString(address, nick);
        editor.commit();
    }

    public void unregister(String address) {
        SharedPreferences.Editor editor = db.edit();
        editor.remove(address);
        editor.commit();
    }

    public boolean isNickRegistered(String nick) {
        return getAll().containsValue(nick);
    }

    public boolean isAddressRegistered(String address) {
        return getAll().containsKey(address);
    }

    public String getNick(String address) {
        return db.getString(address, null);
    }

    public void sendTextMessage(String address, String body) {
        ArrayList<String> parts = smsManager.divideMessage(body);
        sendMultipartTextMessage(address, parts);
    }

    public void sendTextMessageToAll(String body, String exceptAddress) {
        ArrayList<String> parts = smsManager.divideMessage(body);
        for (Map.Entry<String, String> user : getAll().entrySet()) {
            if ((exceptAddress != null) && !exceptAddress.equals(user.getKey())) {
                sendMultipartTextMessage(user.getKey(), parts);
            }
        }
    }

    public void sendMultipartTextMessage(String address, ArrayList<String> parts) {
        smsManager.sendMultipartTextMessage(address, null, parts, null, null);
        String bodyRepr = parts.get(0);
        if (parts.size() > 1) {
            bodyRepr += "...";
        }
        Log.v(TAG, address + " <<< " + bodyRepr);
    }
}




Java Source Code List

com.izgoy.txttr.MainActivity.java
com.izgoy.txttr.SMSHandlerService.java
com.izgoy.txttr.SMSReceiver.java
com.izgoy.txttr.TxttrApplication.java
com.izgoy.txttr.UserManager.java