Android Open Source - sms-smap-gateway M W A P N Utils






From Project

Back to project page sms-smap-gateway.

License

The source code is released under:

GNU General Public License

If you think the Android project sms-smap-gateway 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.android.smap.utils;
//from ww w  .  j a  va2s . com
import com.android.smap.GatewayApp;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.net.Uri;
import android.telephony.TelephonyManager;
import android.util.Log;

public class MWAPNUtils {

  private static final String TAG = MWAPNUtils.class.getCanonicalName();
  // APN related
  public static final Uri APN_TABLE_URI = Uri
      .parse("content://telephony/carriers");
  public static final Uri PREFERRED_APN_URI = Uri
      .parse("content://telephony/carriers/preferapn");

  public static int createAPN(String name, String apnAddr) {
    int id = -1;

    TelephonyManager tele = (TelephonyManager) GatewayApp.getInstance()
        .getSystemService(Context.TELEPHONY_SERVICE);
    String mcc = tele.getSimOperator().substring(0, 3);
    String mnc = tele.getSimOperator().substring(3);

    ContentResolver resolver = GatewayApp.getInstance()
        .getContentResolver();
    ContentValues values = new ContentValues();
    values.put("name", name);
    values.put("apn", apnAddr);

    values.put("mcc", mcc);
    values.put("mnc", mnc);
    values.put("numeric", tele.getSimOperator());

    Cursor cursor = null;

    try {
      Uri newAPN = resolver.insert(APN_TABLE_URI, values);
      if (newAPN != null) {
        cursor = resolver.query(newAPN, null, null, null, null);
        Log.d(TAG, "New APN added.");

        // Obtain the apn id
        int idindex = cursor.getColumnIndex("_id");
        cursor.moveToFirst();
        id = cursor.getShort(idindex);
      }
    } catch (SQLException e) {
      Log.d(TAG, e.getMessage());
    } finally {
      if (cursor != null)
        cursor.close();
    }

    return id;
  }

  public static int getDefaultAPN() {
    int id = -1;
    ContentResolver resolver = GatewayApp.getInstance()
        .getContentResolver();
    Cursor cursor = resolver.query(PREFERRED_APN_URI, new String[] { "_id",
        "name" }, null, null, null);

    if (cursor != null) {
      try {
        if (cursor.moveToFirst()) {
          id = cursor.getInt(cursor.getColumnIndex("_id"));
        }
      } catch (SQLException e) {
        Log.d(TAG, e.getMessage());
      } finally {
        cursor.close();
      }
    }
    return id;
  }

  public static boolean setDefaultAPN(int id) {
    boolean res = false;
    ContentResolver resolver = GatewayApp.getInstance()
        .getContentResolver();
    ContentValues values = new ContentValues();

    values.put("apn_id", id);

    try {
      resolver.update(PREFERRED_APN_URI, values, null, null);
      Cursor cursor = resolver.query(PREFERRED_APN_URI, new String[] {
          "name", "apn" }, "_id=" + id, null, null);
      if (cursor != null) {
        res = true;
        cursor.close();
      }
    } catch (SQLException e) {
      Log.d(TAG, e.getMessage());
    }
    return res;
  }

  public void deleteAPN(int id) {
    ContentResolver resolver = GatewayApp.getInstance()
        .getContentResolver();

    try {
      resolver.delete(APN_TABLE_URI, "_id=?",
          new String[] { Integer.toString(id) });
    } catch (SQLException e) {

    }
  }

  public static void deleteUnfavoriteAPNs() {
    ContentResolver resolver = GatewayApp.getInstance()
        .getContentResolver();

    try {
      resolver.delete(APN_TABLE_URI, "apn LIKE ?",
          new String[] { "relay.nyaruka.com" });
    } catch (SQLException e) {

    }
  }

  public static void tickleDefaultAPN() {
    int id_default = getDefaultAPN();
    int id_fakeAPN = createAPN("SMS Relay", "relay.nyaruka.com");

    Log.d(TAG, "Tickling the Default APN" + id_default + " by fake APN "
        + id_fakeAPN);

    // make the fake APN the default for
    setDefaultAPN(id_fakeAPN);
    nap();

    // switching back to the real working APN
    setDefaultAPN(id_default);

    // and delete the fake APN not in use anymore
    // this.deleteAPN(id_fakeAPN);
    deleteUnfavoriteAPNs();
  }

  /**
   * Take a nap. Sleep 30 seconds to give the network hardware a chance to
   * connect or similar.
   */
  private static void nap() {

    try {
      Thread.sleep(30000);
    } catch (Throwable tt) {
    }

  }

}




Java Source Code List

com.android.smap.AppConfig.java
com.android.smap.GatewayApp.java
com.android.smap.PhoneStateWrapper.java
com.android.smap.activities.BaseActivity.java
com.android.smap.activities.FragmentWrapperActivity.java
com.android.smap.activities.MainActivity.java
com.android.smap.adapters.MenuDrawerAdapter.java
com.android.smap.api.ApiConstants.java
com.android.smap.api.models.Gojo.java
com.android.smap.api.models.Node.java
com.android.smap.api.models.SmapException.java
com.android.smap.api.models.Status.java
com.android.smap.api.models.validator.ModelValidator.java
com.android.smap.api.models.validator.Required.java
com.android.smap.api.requests.ApiRequest.java
com.android.smap.api.requests.GojoRequest.java
com.android.smap.api.requests.StatusRequest.java
com.android.smap.commonsware.wakefull.AlarmReceiver.java
com.android.smap.commonsware.wakefull.WakefulIntentService.java
com.android.smap.controllers.ControllerErrorListener.java
com.android.smap.controllers.ControllerError.java
com.android.smap.controllers.ControllerFactory.java
com.android.smap.controllers.ControllerListener.java
com.android.smap.controllers.Controller.java
com.android.smap.controllers.EmailController.java
com.android.smap.controllers.GojoController.java
com.android.smap.controllers.UIPagingController.java
com.android.smap.controllers.UIRequestController.java
com.android.smap.fragments.BaseFragment.java
com.android.smap.fragments.HomeFragment.java
com.android.smap.models.MenuDrawerItem.java
com.android.smap.models.TextMessage.java
com.android.smap.samuel.Samuel.java
com.android.smap.sms.CellularModem.java
com.android.smap.sms.GatewayService.java
com.android.smap.sms.MonitorService.java
com.android.smap.sms.ScheduleMonitoringListener.java
com.android.smap.sms.ServiceRebooter.java
com.android.smap.ui.ArrayVelocAdapter.java
com.android.smap.ui.OnErrorTouchedListener.java
com.android.smap.ui.Operation.java
com.android.smap.ui.VelocAdapter.java
com.android.smap.ui.ViewBinder.java
com.android.smap.ui.ViewQuery.java
com.android.smap.ui.views.AnimatedNetworkImageView.java
com.android.smap.ui.views.ErrorView.java
com.android.smap.ui.views.FontableTextView.java
com.android.smap.utils.BitmapLruCache.java
com.android.smap.utils.MWAPNUtils.java
com.android.smap.utils.MWAnimUtil.java
com.android.smap.utils.MWAssetsUtils.java
com.android.smap.utils.MWConnectionUtils.java
com.android.smap.utils.MWEmailUtils.java
com.android.smap.utils.MWUiUtils.java
com.android.smap.utils.PreferenceWrapper.java
com.android.smap.utils.Refresher.java
com.android.smap.utils.UriBuilder.java