package com.ziang.driver.base; import android.annotation.SuppressLint; import android.content.Context; import android.content.Intent; import android.content.res.Configuration; import android.content.res.Resources; import android.graphics.drawable.Drawable; import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.TextView; import androidx.annotation.LayoutRes; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentTransaction; import com.ziang.driver.R; import com.ziang.driver.ui.DialogUtil; import com.ziang.driver.ui.main.GetOrderFragment; import com.ziang.driver.ui.main.TripActivity; import com.ziang.driver.ui.main.VoiceOrderFragment; import com.ziang.driver.utils.ActivityCollector; import com.ziang.driver.utils.EasePopup.EasyPopup; import com.github.zackratos.ultimatebar.UltimateBar; import com.google.gson.Gson; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; import java.util.List; import cn.sinata.xldutils.activitys.BaseActivity; import pub.devrel.easypermissions.EasyPermissions; import static android.view.View.GONE; import static android.view.View.VISIBLE; /** * Created by Administrator on 2018/1/16. */ public abstract class MyBaseActivity extends BaseActivity implements EasyPermissions.PermissionCallbacks { public static final boolean D = true; public static final String TAG = "MyBaseActivity"; //用户token private LinearLayout title_layout; public RelativeLayout rl_title, rl_header; public TextView tv_Left, tv_Right; public TextView tv_title; public TextView tv_test; public View view_bottom_title; public ImageView mIvRight, iv_right_title_two; private boolean hasTitle; public static final int PAGE_SIZE = 10;//每页条数 public int pageNumber = 1;//当前第几页 private boolean isBlackshow = true;//判断是否显示深色导航栏字体 Bundle bundle; Intent intent; public Gson gson; public EasyPopup easyPopup; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); initParam(); getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); EventBus.getDefault().register(this); ActivityCollector.addActivity(this, getClass()); ActivityCollector.getActivityCollector().addActivity(this); setTouMing(); initData(); setBlcakShow(isBlackshow); setContentView(); initView(); setOnclick(); settest(); } Handler heartHandler; private void settest() { // tv_test.setVisibility(VISIBLE); // if (MyApplication.Companion.getLocation().getLatitude() != -1.0){ // tv_test.setText( // "定位位置:lat="+MyApplication.Companion.getLocation().getLatitude()+" lng="+MyApplication.Companion.getLocation().getLongitude() // + " 心跳数量()="+MyApplication.Companion.getTestHeartNunm()+" 收到的websocket数量="+MyApplication.Companion.getTestReceiveNum() // +" 方向角="+MyApplication.Companion.getLocation().getBearing()+" 海拔"+MyApplication.Companion.getLocation().getAltitude() // ); // } // if (heartHandler == null) { // heartHandler = new Handler(getMainLooper()); // } // heartHandler.postDelayed(this::settest,1000); } @Override public void showDialog() { if (easyPopup == null) { easyPopup = DialogUtil.INSTANCE.getPopupwindow(this, R.layout.progress_dialog_main); } if (easyPopup.isShowing()) { return; } getWindow().getDecorView().post(showPopRun); } Runnable showPopRun = new Runnable() { @Override public void run() { easyPopup.showCenter(getWindow().getDecorView()); } }; public void closePop() { if (easyPopup != null) { getWindow().getDecorView().removeCallbacks(showPopRun); if (easyPopup.isShowing()) { easyPopup.dismiss(); } } } /*** * type 1:普通单 2:该派单 * @param orderId * @param orderType * @param time * @param type */ public void showOrder(String orderId, String orderType, int time, int type,String audio) { // MyApplication.Companion.getTTsManager().setVideoText("子昂出行司机,有一条新的订单"); Intent intent; if (audio.isEmpty()) intent = new Intent(this, GetOrderFragment.class); else { intent = new Intent(this, VoiceOrderFragment.class); intent.putExtra("audio", audio); } intent.putExtra("orderId", orderId); intent.putExtra("orderType", orderType); intent.putExtra("time", time); intent.putExtra("type", type); startActivity(intent); } /*** * 子昂新:获取到数据后的订单 * type 1:普通单 2:该派单 * @param orderId * @param orderType * @param time * @param type */ public void showOrderWithData(String orderId, String orderType, int time, int type,String data) { // MyApplication.Companion.getTTsManager().setVideoText("子昂出行司机,有一条新的订单"); Intent intent = new Intent(this, GetOrderFragment.class); intent.putExtra("orderId", orderId); intent.putExtra("orderType", orderType); intent.putExtra("time", time); intent.putExtra("type", type); intent.putExtra("data", data); startActivity(intent); } /*** * 扫码 * @param orderId * @param orderType */ public void toDorderAct(String orderId, String orderType) { MyApplication.Companion.getTTsManager().setVideoText("子昂出行司机,有一条新的订单"); Intent intent = new Intent(this, TripActivity.class); intent.putExtra("orderId", orderId); intent.putExtra("orderType", orderType); startActivity(intent); } @Override protected void onPause() { super.onPause(); closePop(); } @Override public void dismissDialog() { closePop(); } protected void initParam() { gson = new Gson(); intent = getIntent(); bundle = intent == null ? null : intent.getExtras(); } private void setTouMing() { UltimateBar.Companion.with(this) .statusDrawable2(getResources().getDrawable(R.drawable.bg_lucency)) // Android 6.0 以下状态栏灰色模式时状态栏颜色 .applyNavigation(true) // 应用到导航栏,默认 flase .navigationDark(false) // 导航栏灰色模式(Android 8.0+),默认 false // .navigationDrawable2(getResources().getDrawable(R.drawable.bg_gray)) // Android 8.0 以下导航栏灰色模式时导航栏颜色 .create() .immersionBar(); } public void startActivity(Class tClass) { startActivity(new Intent(this, tClass)); } public void startActivity(Class tClass, Bundle bundle) { if (bundle != null) { Intent intent = new Intent(this, tClass); intent.putExtras(bundle); startActivity(intent); } else { startActivity(tClass); } } public void startActivityResult(Class tClass, int code) { Intent intent = new Intent(this, tClass); startActivityForResult(intent, code); } public void initData() { } /** * 设置导航栏字体颜色 黑色 true / 白色 false * * @param isBlack */ public void setBlcakShow(boolean isBlack) { getWindow().getDecorView().setBackgroundResource(R.color.white); if (isBlack) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { //设置状态栏文字颜色及图标为深色 this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); } } else { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { //设置状态栏文字颜色及图标为浅色 getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); } } } @Subscribe(threadMode = ThreadMode.MAIN) public void onEventMainThread(BaseEvent event) { switch (event.getCode()) { case BaseEvent.OUT_APP: finish(); break; } } public abstract void setContentView(); public abstract void initView(); public abstract void setOnclick(); @Override protected void onDestroy() { super.onDestroy(); ActivityCollector.removeActivity(this); ActivityCollector.getActivityCollector().finishActivity(this); EventBus.getDefault().unregister(this); } // @Override // protected boolean shouldPortrait() { // return isShouldPortrait(); // } protected boolean isShouldPortrait() { return true; } /** * 获取string数据 * * @param key * @return */ public String getBundleString(String key) { if (bundle != null) { return bundle.getString(key); } return ""; } /** * 获取int数据 * * @param key * @return */ public int getBundleInt(String key, int defaultValue) { if (bundle != null) { return bundle.getInt(key, defaultValue); } return defaultValue; } public Bundle creatBundleString(String key, String value) { Bundle bundle = new Bundle(); bundle.putString(key, value); return bundle; } public Object getBundleSerializable(String key) { if (bundle != null) { return bundle.getSerializable(key); } return null; } public boolean getBundleBoolean(String key, boolean defaultValue) { if (bundle != null) { return bundle.getBoolean(key, defaultValue); } return defaultValue; } //设置字体不随系统改变 @Override public void onConfigurationChanged(Configuration newConfig) { if (newConfig.fontScale != 1)//非默认值 { getResources(); } super.onConfigurationChanged(newConfig); } @Override public Resources getResources() { Resources res = super.getResources(); if (res.getConfiguration().fontScale != 1) { //非默认值 Configuration newConfig = new Configuration(); newConfig.setToDefaults();//设置默认 res.updateConfiguration(newConfig, res.getDisplayMetrics()); } return res; } /** * 结束页面的时候将页面中相关控件移除 * http://blog.csdn.net/newlai913/article/details/78095873 */ @Override public void finish() { super.finish(); } @SuppressLint("ResourceType") @Override public void setContentView(@LayoutRes int layoutResID) { super.setContentView(R.layout.app_title); init(); if (layoutResID > 0) { LayoutInflater.from(this).inflate(layoutResID, title_layout, true); } } @Override public void setContentView(View view) { super.setContentView(view); init(); if (view != null) { title_layout.addView(view); } } @Override public void setContentView(View view, ViewGroup.LayoutParams params) { super.setContentView(view, params); init(); if (view != null) { title_layout.addView(view, params); } } private void init() { title_layout = (LinearLayout) findViewById(R.id.title_layout); rl_title = (RelativeLayout) findViewById(R.id.rl_title); tv_Left = (TextView) findViewById(R.id.tv_left_title); tv_Right = (TextView) findViewById(R.id.tv_right_title); rl_header = (RelativeLayout) findViewById(R.id.rl_header_title); tv_title = (TextView) findViewById(R.id.tv_title); view_bottom_title = findViewById(R.id.view_bottom_title); mIvRight = findViewById(R.id.iv_right_title); iv_right_title_two = findViewById(R.id.iv_right_title_two); tv_test = findViewById(R.id.tv_test_content); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { rl_header.setBackgroundResource(R.color.gray); } setBack(new View.OnClickListener() { @Override public void onClick(View v) { onBackPressed(); } }); } @Override public boolean onTouchEvent(@NonNull MotionEvent event) { if (null != this.getCurrentFocus()) { /** * 点击空白位置 隐藏软键盘 */ InputMethodManager mInputMethodManager = null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE) { mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE) { if (mInputMethodManager != null) { return mInputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0); } } } return super.onTouchEvent(event); } /** * 是否显示title * * @param hasTitle */ public void showTitle(boolean hasTitle) { rl_title.setVisibility(hasTitle ? VISIBLE : GONE); } public ImageView getmIvRight() { return mIvRight; } public ImageView getIv_right_title_two() { return iv_right_title_two; } public void setBack(View.OnClickListener onClickListener) { tv_Left.setOnClickListener(onClickListener); } /** * 是否显示返回键 * * @param v */ public void showBack(int v) { tv_Left.setVisibility(v); } /** * 是否显示白色状态栏 * * @param hasHeader 是否显示头部 */ public void showHeader(boolean hasHeader) { rl_header.setVisibility(hasHeader ? VISIBLE : GONE); } /** * 设置title文字 * * @param titleId */ public void setTitleText(int titleId) { tv_title.setText(titleId); } /** * 设置title文字 * * @param title */ public void setTitleText(String title) { tv_title.setText(title); } //设置title点击事件 public void setTitleOnClicker(View.OnClickListener onClicker) { tv_title.setOnClickListener(onClicker); } //设置title图片资源id public void setTitleShow(int ResId) { Drawable nav_up = getResources().getDrawable(ResId); nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight()); tv_title.setCompoundDrawables(null, null, nav_up, null); } /** * 设置左边按钮文字 * * @param titleId */ public void setLeftText(int titleId) { tv_Left.setText(titleId); } public void setRightImg(Drawable drawable) { tv_Right.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null); } /** * 设置右边按钮文字 * * @param titleId */ public void setRightText(String titleId) { tv_Right.setText(titleId); } public void setRightClickerable(boolean clickerable) { tv_Right.setEnabled(clickerable); } public void setRightButton(View.OnClickListener onClickListener) { tv_Right.setOnClickListener(onClickListener); } /** * 设置是否显示分割线 */ public void setBottomShow(boolean isShow) { if (isShow) { view_bottom_title.setVisibility(View.VISIBLE); } else { view_bottom_title.setVisibility(View.INVISIBLE); } } /*** * 添加一个fragment 到栈 * @param fragment 目标类 * @param container 容器id */ public void addFragment(Fragment fragment, int container) { FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); if (!fragment.isAdded()) { fragmentTransaction.add(container, fragment); } for (Fragment currentFragment : getSupportFragmentManager().getFragments()) { if (currentFragment.isAdded() && currentFragment != fragment) { fragmentTransaction.hide(currentFragment); } } fragmentTransaction.show(fragment); fragmentTransaction.commitAllowingStateLoss(); getSupportFragmentManager().executePendingTransactions(); } // 获取权限 public boolean checkPermissions(String... per) { if (EasyPermissions.hasPermissions(this, per)) { //已经打开权限 return true; } else { return false; } } //成功打开权限 @Override public void onPermissionsGranted(int requestCode, @NonNull List perms) { } //用户未同意权限 @Override public void onPermissionsDenied(int requestCode, @NonNull List perms) { } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this); } public void closeSoftKeybord(EditText mEditText, Context mContext) { InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE); if (imm != null) imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0); } }