package com.uiuios.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.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.hjq.toast.Toaster; import com.tencent.mmkv.MMKV; import com.uiuios.aios.R; import com.uiuios.aios.bean.BaseResponse; import com.uiuios.aios.bean.Contact; import com.uiuios.aios.config.CommonConfig; import com.uiuios.aios.disklrucache.CacheHelper; import com.uiuios.aios.manager.AmapManager; import com.uiuios.aios.network.NetInterfaceManager; import com.uiuios.aios.network.UrlAddress; import com.uiuios.aios.receiver.BootReceiver; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; 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 { private MMKV mMMKV = MMKV.mmkvWithID(CommonConfig.MMKV_ID, MMKV.MULTI_PROCESS_MODE); @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; // private CacheHelper mCacheHelper; @Override protected void onCreate(Bundle savedInstanceState) { Log.e(TAG, "onCreate: "); super.onCreate(savedInstanceState); setContentView(R.layout.activity_emergency); // mCacheHelper = new CacheHelper(this); ButterKnife.bind(this); //发送紧急广播 Intent bootIntent = new Intent(BootReceiver.SOS); bootIntent.setComponent(new ComponentName("com.uiuios.sn", "com.uiuios.sn.receiver.BootReceiver")); sendBroadcast(bootIntent); AmapManager.getInstance().startLocation(); Intent intent = getIntent(); // String contactList = intent.getStringExtra("contactList"); // if (TextUtils.isEmpty(contactList)) { // finish(); // return; // } // String jsonString = mCacheHelper.getAsString(UrlAddress.GET_MAIL_LIST); // //为 "" 是已经请求成功的 // if (jsonString == null) { // return; // } // Gson gson = new Gson(); // Type type = new TypeToken>() { // }.getType(); // List contactList = gson.fromJson(jsonString, type); // if (contactList == null || contactList.size() == 0) { // return; // } String phone = mMMKV.decodeString(CommonConfig.EMERGENCY_CONTACT_KEY, ""); // phoneListSet = new ArrayList(); // 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 { String longitude = mMMKV.decodeString(CommonConfig.MAP_LONGITUDE_KEY, ""); String latitude = mMMKV.decodeString(CommonConfig.MAP_LATITUDE_KEY, ""); String address = mMMKV.decodeString(CommonConfig.MAP_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; if (TextUtils.isEmpty(phone)) { Toaster.show("没有设置紧急联系人"); finish(); } Intent intent1 = new Intent(Intent.ACTION_CALL); Uri data = Uri.parse("tel:" + phone); intent1.setData(data); startActivity(intent1); isCalling = true; // phoneListSet.remove(0); subscribe.dispose(); finish(); } }); 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; // } // new Handler().postDelayed(new Runnable() { // @Override // public void run() { // Intent intent1 = new Intent(Intent.ACTION_CALL); // String phone = phoneListSet.get(0).getMobile(); // Uri data = Uri.parse("tel:" + phone); // intent1.setData(data); // startActivity(intent1); // isCalling = true; // phoneListSet.remove(0); // } // }, 2000); // } else { // if (phoneListSet == null) { // Toaster.show("没有设置紧急联系人"); // finish(); // } // } } @Override protected void onDestroy() { super.onDestroy(); Log.e(TAG, "onDestroy: "); if (subscribe != null && !subscribe.isDisposed()) { 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; //正在挂断 // } }