package com.uiui.aios.activity; import androidx.appcompat.app.AppCompatActivity; import android.content.ComponentName; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.provider.Settings; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import com.tencent.mmkv.MMKV; import com.uiui.aios.R; import com.uiui.aios.bean.BaseResponse; import com.uiui.aios.manager.AmapManager; import com.uiui.aios.network.NetInterfaceManager; import com.uiui.aios.receiver.BootReceiver; import com.uiui.aios.utils.ToastUtil; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.concurrent.TimeUnit; import butterknife.BindView; import butterknife.ButterKnife; import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers; import io.reactivex.rxjava3.annotations.NonNull; import io.reactivex.rxjava3.core.Observable; import io.reactivex.rxjava3.core.Observer; import io.reactivex.rxjava3.disposables.Disposable; import io.reactivex.rxjava3.schedulers.Schedulers; public class EmergencyActivity extends AppCompatActivity { @BindView(R.id.tv_countdown) TextView tv_countdown; @BindView(R.id.iv_finish) ImageView iv_finish; private Disposable subscribe; private String TAG = EmergencyActivity.class.getSimpleName(); private boolean needDial = false; private boolean isCalling = false; private List phoneListSet; @Override protected void onCreate(Bundle savedInstanceState) { Log.e(TAG, "onCreate: "); super.onCreate(savedInstanceState); setContentView(R.layout.activity_emergency); ButterKnife.bind(this); //发送紧急广播 Intent bootIntent = new Intent(BootReceiver.SOS); bootIntent.setComponent(new ComponentName("com.uiui.sn", "com.uiui.sn.receiver.BootReceiver")); sendBroadcast(bootIntent); Intent intent = getIntent(); // String setting_sos = intent.getStringExtra("setting_sos"); // if (TextUtils.isEmpty(setting_sos)) { // finish(); // return; // } String setting_sos = Settings.System.getString(getContentResolver(), "setting_sos"); if (TextUtils.isEmpty(setting_sos)) return; phoneListSet = new ArrayList<>(Arrays.asList(setting_sos.split(","))); Log.e(TAG, "onCreate: " + phoneListSet); final Long time = 3L; subscribe = Observable.interval(1, TimeUnit.SECONDS) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(aLong -> { Log.e("TAG", "倒计时:" + aLong); if (aLong < time && !subscribe.isDisposed()) { tv_countdown.setText("倒计时(" + (time - aLong - 1) + ")秒"); Log.e(TAG, "accept: " + (time - aLong - 1)); } else { MMKV mmkv = MMKV.defaultMMKV(); String longitude = mmkv.decodeString(AmapManager.LONGITUDE_KEY, "未知"); String latitude = mmkv.decodeString(AmapManager.LATITUDE_KEY, "未知"); String address = mmkv.decodeString(AmapManager.ADDRESS_KEY, "未知"); NetInterfaceManager.getInstance() .getSOSRecordObservable(longitude, latitude, address) .subscribe(new Observer() { @Override public void onSubscribe(@NonNull Disposable d) { Log.e("SOSRecordApi", "onSubscribe: "); } @Override public void onNext(@NonNull BaseResponse baseResponse) { Log.e("SOSRecordApi", "onNext: " + baseResponse); } @Override public void onError(@NonNull Throwable e) { Log.e("SOSRecordApi", "onError: " + e.getMessage()); } @Override public void onComplete() { Log.e("SOSRecordApi", "onComplete: "); } }); needDial = true; Intent intent1 = new Intent(Intent.ACTION_CALL); Uri data = Uri.parse("tel:" + phoneListSet.get(0)); intent1.setData(data); startActivity(intent1); isCalling = true; phoneListSet.remove(0); // finish(); subscribe.dispose(); } }); iv_finish.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); // TelephonyManager telM = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); // telM.listen(new PhoneStateListener() { // /** // * 当有精确通话状态时回调 // * Callback invoked when precise device call state changes // * @hide 隐藏api, 给系统app使用的 // */ // public void onPreciseCallStateChanged(PreciseCallState callState) { // //当有精确通话状态时回调 // Log.e(TAG, "onPreciseCallStateChanged: " + callState); // } // }, PhoneStateListener.LISTEN_PRECISE_CALL_STATE); //需要权限android.permission.READ_PRECISE_PHONE_STATE // // StatusBarManager mStatusBarManager = (StatusBarManager) getApplicationContext().getSystemService(Context.STATUS_BAR_SERVICE); //// mStatusBarManager.disable(StatusBarManager.DISABLE_HOME);//隐藏home键 } @Override protected void onStart() { super.onStart(); Log.e(TAG, "onResume: "); Log.e(TAG, "onResume: " + phoneListSet); if (needDial) { if (phoneListSet == null || phoneListSet.size() == 0) return; Handler.getMain().postDelayed(new Runnable() { @Override public void run() { Intent intent1 = new Intent(Intent.ACTION_CALL); Uri data = Uri.parse("tel:" + phoneListSet.get(0)); intent1.setData(data); startActivity(intent1); isCalling = true; phoneListSet.remove(0); } }, 2000); } else { if (phoneListSet == null) { ToastUtil.show("没有设置紧急联系人"); finish(); } } } @Override protected void onDestroy() { super.onDestroy(); Log.e(TAG, "onDestroy: "); if (subscribe != null) { subscribe.dispose(); subscribe = null; } } // class MobileCallReceiver extends BroadcastReceiver { // @Override // public void onReceive(Context context, Intent intent) { // if (intent.getAction() == null) { // return; // } //// switch (intent.getAction()) { //// case CustomAction.PRECISE_CALL_STATE: //// int callState = intent.getIntExtra("foreground_state", -2); //// switch (callState) { //// case PreciseCallState.PRECISE_CALL_STATE_IDLE: //// Log.d(TAG, "IDLE"); //// break; //// case PreciseCallState.PRECISE_CALL_STATE_DIALING: //// Log.d(TAG, "DIALING"); //// break; //// case PreciseCallState.PRECISE_CALL_STATE_ALERTING: //// Log.d(TAG, "ALERTING isHandFree=" + isHandFree); //// break; //// case PreciseCallState.PRECISE_CALL_STATE_ACTIVE: //// Log.d(TAG, "ACTIVE"); //// break; //// case PreciseCallState.PRECISE_CALL_STATE_INCOMING: //// Log.d(TAG, "INCOMING来电"); //// break; //// case PreciseCallState.PRECISE_CALL_STATE_DISCONNECTING: //// Log.d(TAG, "DISCONNECTING"); //// break; //// case PreciseCallState.PRECISE_CALL_STATE_DISCONNECTED: //// Log.d(TAG, "DISCONNECTED"); //// break; //// } //// break; //// case TelephonyManager.ACTION_PHONE_STATE_CHANGED: //// break; //// } // } // } // // // 精确的九大通话状态 // public class PreciseCallState { // public static final int PRECISE_CALL_STATE_IDLE = 0; //通话空闲 // public static final int PRECISE_CALL_STATE_ACTIVE = 1; //正在通话(活动中) // public static final int PRECISE_CALL_STATE_HOLDING = 2; //通话挂起(例如我和多个人通话,其中一个通话在活动,而其它通话就会进入挂起状态) // public static final int PRECISE_CALL_STATE_DIALING = 3; //拨号开始 // public static final int PRECISE_CALL_STATE_ALERTING = 4; //正在呼出(提醒对方接电话) // public static final int PRECISE_CALL_STATE_INCOMING = 5; //对方来电 // public static final int PRECISE_CALL_STATE_WAITING = 6; //第三方来电等待(例如我正在和某人通话,而其他人打入时就会就进入等待状态) // public static final int PRECISE_CALL_STATE_DISCONNECTED = 7; //挂断完成 // public static final int PRECISE_CALL_STATE_DISCONNECTING = 8; //正在挂断 // } }