move SMSPhoneInfo into its own class/file

This commit is contained in:
Eric House 2019-04-02 14:47:15 -07:00
parent 5e1e285046
commit 758f08c7f3
8 changed files with 111 additions and 84 deletions

View file

@ -782,7 +782,7 @@ public class GameConfigDelegate extends DelegateBase
private void showConnAfterCheck()
{
if ( null == NBSProto.getPhoneInfo( m_activity ) ) {
if ( null == SMSPhoneInfo.get( m_activity ) ) {
Perms23.tryGetPerms( this, Perms23.Perm.READ_PHONE_STATE,
R.string.phone_state_rationale,
Action.ASKED_PHONE_STATE );

View file

@ -2230,7 +2230,7 @@ public class GamesListDelegate extends ListDelegateBase
int bits = extras.getInt( REMATCH_ADDRS_EXTRA, -1 );
final CommsConnTypeSet addrs = new CommsConnTypeSet( bits );
boolean hasSMS = addrs.contains( CommsConnType.COMMS_CONN_SMS );
if ( !hasSMS || null != NBSProto.getPhoneInfo( m_activity ) ) {
if ( !hasSMS || null != SMSPhoneInfo.get( m_activity ) ) {
rematchWithNameAndPerm( gameName, addrs );
} else {
int id = (1 == addrs.size())

View file

@ -25,7 +25,6 @@ import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneNumberUtils;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;
import java.util.HashMap;
import java.util.HashSet;
@ -92,81 +91,6 @@ public class NBSProto {
s_showToasts = newVal;
}
public static class SMSPhoneInfo {
public SMSPhoneInfo( boolean isAPhone, String num, boolean gsm ) {
isPhone = isAPhone;
number = num;
isGSM = gsm;
}
public boolean isPhone;
public String number;
public boolean isGSM;
}
private static SMSPhoneInfo s_phoneInfo;
public static SMSPhoneInfo getPhoneInfo( Context context )
{
if ( null == s_phoneInfo ) {
try {
String number = null;
boolean isGSM = false;
boolean isPhone = false;
TelephonyManager mgr = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
if ( null != mgr ) {
number = mgr.getLine1Number(); // needs permission
int type = mgr.getPhoneType();
isGSM = TelephonyManager.PHONE_TYPE_GSM == type;
isPhone = true;
}
String radio =
XWPrefs.getPrefsString( context, R.string.key_force_radio );
int[] ids = { R.string.radio_name_real,
R.string.radio_name_tablet,
R.string.radio_name_gsm,
R.string.radio_name_cdma,
};
// default so don't crash before set
int id = R.string.radio_name_real;
for ( int ii = 0; ii < ids.length; ++ii ) {
if ( radio.equals(context.getString(ids[ii])) ) {
id = ids[ii];
break;
}
}
switch( id ) {
case R.string.radio_name_real:
break; // go with above
case R.string.radio_name_tablet:
number = null;
isPhone = false;
break;
case R.string.radio_name_gsm:
case R.string.radio_name_cdma:
isGSM = id == R.string.radio_name_gsm;
if ( null == number ) {
number = "000-000-0000";
}
isPhone = true;
break;
}
s_phoneInfo = new SMSPhoneInfo( isPhone, number, isGSM );
} catch ( SecurityException se ) {
Log.e( TAG, "got SecurityException" );
}
}
return s_phoneInfo;
}
public static void resetPhoneInfo()
{
s_phoneInfo = null;
}
static abstract class NBSProtoThread extends Thread {
private String mPhone;
private short mPort;
@ -305,7 +229,7 @@ public class NBSProto {
// Try send-to-self
if ( XWPrefs.getSMSToSelfEnabled( context ) ) {
String myPhone = getPhoneInfo( context ).number;
String myPhone = SMSPhoneInfo.get( context ).number;
if ( null != myPhone
&& PhoneNumberUtils.compare( phone, myPhone ) ) {
for ( byte[] fragment : fragments ) {

View file

@ -591,7 +591,7 @@ public class NetLaunchInfo implements Serializable {
public void addSMSInfo( Context context )
{
NBSProto.SMSPhoneInfo pi = NBSProto.getPhoneInfo( context );
SMSPhoneInfo pi = SMSPhoneInfo.get( context );
if ( null != pi ) {
phone = pi.number;
isGSM = pi.isGSM;

View file

@ -224,7 +224,7 @@ public class PrefsDelegate extends DelegateBase
forceDictsMatch( sp.getString( key, null ) );
break;
case R.string.key_force_radio:
NBSProto.resetPhoneInfo();
SMSPhoneInfo.reset();
break;
case R.string.key_disable_nag:
case R.string.key_disable_nag_solo:

View file

@ -0,0 +1,103 @@
/* -*- compile-command: "find-and-gradle.sh inXw4Deb"; -*- */
/*
* Copyright 2010 - 2018 by Eric House (xwords@eehouse.org). All rights
* reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.eehouse.android.xw4;
import android.content.Context;
import android.telephony.TelephonyManager;
public class SMSPhoneInfo {
private static final String TAG = SMSPhoneInfo.class.getSimpleName();
public boolean isPhone;
public String number;
public boolean isGSM;
public SMSPhoneInfo( boolean isAPhone, String num, boolean gsm )
{
isPhone = isAPhone;
number = num;
isGSM = gsm;
}
private static SMSPhoneInfo s_phoneInfo;
public static SMSPhoneInfo get( Context context )
{
if ( null == s_phoneInfo ) {
try {
String number = null;
boolean isGSM = false;
boolean isPhone = false;
TelephonyManager mgr = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
if ( null != mgr ) {
number = mgr.getLine1Number(); // needs permission
int type = mgr.getPhoneType();
isGSM = TelephonyManager.PHONE_TYPE_GSM == type;
isPhone = true;
}
String radio =
XWPrefs.getPrefsString( context, R.string.key_force_radio );
int[] ids = { R.string.radio_name_real,
R.string.radio_name_tablet,
R.string.radio_name_gsm,
R.string.radio_name_cdma,
};
// default so don't crash before set
int id = R.string.radio_name_real;
for ( int ii = 0; ii < ids.length; ++ii ) {
if ( radio.equals(context.getString(ids[ii])) ) {
id = ids[ii];
break;
}
}
switch( id ) {
case R.string.radio_name_real:
break; // go with above
case R.string.radio_name_tablet:
number = null;
isPhone = false;
break;
case R.string.radio_name_gsm:
case R.string.radio_name_cdma:
isGSM = id == R.string.radio_name_gsm;
if ( null == number ) {
number = "000-000-0000";
}
isPhone = true;
break;
}
s_phoneInfo = new SMSPhoneInfo( isPhone, number, isGSM );
} catch ( SecurityException se ) {
Log.e( TAG, "got SecurityException" );
}
}
return s_phoneInfo;
}
public static void reset()
{
s_phoneInfo = null;
}
}

View file

@ -126,7 +126,7 @@ public class Utils {
{
boolean result = false;
if ( Perms23.havePermissions( context, Perm.READ_PHONE_STATE ) ) {
NBSProto.SMSPhoneInfo info = NBSProto.getPhoneInfo( context );
SMSPhoneInfo info = SMSPhoneInfo.get( context );
result = null != info && info.isPhone && info.isGSM;
}
Log.d( TAG, "isGSMPhone() => %b", result );

View file

@ -29,7 +29,7 @@ import org.eehouse.android.xw4.BuildConfig;
import org.eehouse.android.xw4.GameUtils;
import org.eehouse.android.xw4.Log;
import org.eehouse.android.xw4.R;
import org.eehouse.android.xw4.NBSProto;
import org.eehouse.android.xw4.SMSPhoneInfo;
import org.eehouse.android.xw4.Utils;
import org.eehouse.android.xw4.WiDirService;
import org.eehouse.android.xw4.WiDirWrapper;
@ -373,7 +373,7 @@ public class CommsAddrRec {
}
break;
case COMMS_CONN_SMS:
NBSProto.SMSPhoneInfo pi = NBSProto.getPhoneInfo( context );
SMSPhoneInfo pi = SMSPhoneInfo.get( context );
// Do we have phone permission? If not, shouldn't be set at all!
if ( null != pi ) {
sms_phone = pi.number;