Java tutorial
package com.example.haber.ui.activity; import android.app.Activity; import android.app.ActivityManager; import android.app.Fragment; import android.app.FragmentManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Build; import android.os.Handler; import android.os.Vibrator; import android.support.annotation.RequiresApi; import android.support.constraint.ConstraintLayout; import android.support.design.widget.NavigationView; import android.support.v13.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.widget.Toolbar; import android.util.DisplayMetrics; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.animation.Animation; import android.view.animation.TranslateAnimation; import android.widget.Button; import android.widget.ImageButton; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.ToggleButton; import com.example.haber.R; import com.example.haber.biz.CallOutBiz; import com.example.haber.biz.GeneralSettingBiz; import com.example.haber.biz.PanBiz; import com.example.haber.ui.Adapter.MyViewPagerFragmentAdapter; import com.example.haber.ui.BaseActivity; import com.example.haber.ui.MyApplication; import com.example.haber.ui.listense.MyOnNavigationItemSelectedListener; import com.example.haber.util.Const; import com.example.haber.util.ExceptionUtil; import com.example.haber.util.HaberManagerUtil; import com.example.haber.util.LogUtil; import com.example.haber.util.Tools; import java.util.List; import static android.R.attr.filter; public class TabbedActivity extends BaseActivity { /** * The {@link android.support.v4.view.PagerAdapter} that will provide * fragments for each of the sections. We use a * {@link FragmentPagerAdapter} derivative, which will keep every * loaded fragment in memory. If this becomes too memory intensive, it * may be best to switch to a * {@link android.support.v13.app.FragmentStatePagerAdapter}. */ /** * The {@link ViewPager} that will host the section contents. */ private String TAG = "TabbedActivity"; private ViewPager mViewPager; private Toolbar toolbar; private int currentPosition = 0; private ImageButton ibHangUp; private MyBroadCaseReceiver receiver; private ToggleButton tbCont; private ToggleButton tbDial; private ToggleButton tbMute; private ToggleButton tbConsoleControl; private ToggleButton tbRecode; private ToggleButton tbXX; private LinearLayout menuHeartLayout; private TextView tvSerialNo; private DrawerLayout drawer; private NavigationView navigationView; private ConstraintLayout clBasic; private ActionBar actionBar; private LinearLayout layoutDial; private boolean movePanRemote = false; private static final int DIAL_STATUS = 100; private static final int CONSOLE_CONTROL_STATUS = 101; private static final int COMMON_STATUS = 102; private int currentStatus = COMMON_STATUS; private View tvLine; private int displayWidth; private DisplayMetrics metrics; private int displayHeight; private ConstraintLayout containViewPager; private ConstraintLayout containConsoleControl; private ImageButton btnPanDown; private ImageButton btnPanUp; private ImageButton btnPanLeft; private ImageButton btnPanRight; private ImageButton btnPanZoomIn; private ImageButton btnPanZoomOut; private TextView tvSwitcherPan; private SensorManager sensorManager; private Vibrator vibrator; private Sensor sensor; private SensorEventListener sensorListener; private long lastTime; private ActivityManager activityManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_hangup_drawer); // Create the adapter that will return a fragment for each of the three // primary sections of the activity. // Set up the ViewPager with the sections adapter. mViewPager = (ViewPager) findViewById(R.id.viewPage_container_hangup); mViewPager.setAdapter(new MyViewPagerFragmentAdapter(getSupportFragmentManager())); try { setViews(); initialize(); addListener(); } catch (Exception e) { ExceptionUtil.handlerException(e); } } @Override protected void onDestroy() { super.onDestroy(); } private IntentFilter filter; private void initialize() { activityManager = (ActivityManager) TabbedActivity.this.getSystemService(ACTIVITY_SERVICE); metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); displayWidth = metrics.widthPixels; displayHeight = metrics.heightPixels; receiver = new MyBroadCaseReceiver(); filter = new IntentFilter(Const.CALL_STATUS); filter.addAction(Const.ACTION_TASK_STATUS); filter.addAction(Const.ACTION_UPDATA_LICENSE_INFO); } private void iniSystemService() { sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); if (sensorManager == null) return; sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); } private float lastX; private float lastY; private float lastZ; private static final int SENSOR_SENSITIVITY = 30; private long lastSwichPanTime; @Override protected void onResume() { super.onResume(); lastTime = System.currentTimeMillis(); try { sensorListener = new SensorEventListener() { @Override public void onSensorChanged(SensorEvent event) { try { long currentTime = System.currentTimeMillis(); long intevalTime = currentTime - lastTime; if (intevalTime < 50) return; lastTime = currentTime; float[] values = event.values; float x = values[0]; float y = values[1]; float z = values[2]; float detaX = x - lastX; float detaY = y - lastY; float detaZ = z - lastZ; lastX = x; lastY = y; lastZ = z; if ((Math.sqrt(detaX * detaX + detaY * detaY + detaZ * detaZ) / intevalTime) * 100 < SENSOR_SENSITIVITY) return; if (vibrator == null) return; vibrator.vibrate(300); long currentSwitchPanTime = System.currentTimeMillis(); if (currentSwitchPanTime - lastSwichPanTime < 1000) return; lastSwichPanTime = currentSwitchPanTime; switchPan(); } catch (Exception e) { ExceptionUtil.handlerException(e); } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } }; if (currentStatus == CONSOLE_CONTROL_STATUS) { if (sensor == null) return; sensorManager.registerListener(sensorListener, sensor, SensorManager.SENSOR_DELAY_UI); } handlerCallStatus(); } catch (Exception e) { ExceptionUtil.handlerException(e); } } @Override protected void onPause() { super.onPause(); try { sensorManager.unregisterListener(sensorListener, sensor); } catch (Exception e) { ExceptionUtil.handlerException(e); } } private void switchStatus() { if (currentStatus == COMMON_STATUS) { if (sensorManager != null) sensorManager.unregisterListener(sensorListener, sensor); if (tbDial.isChecked()) tbDial.setChecked(false); if (tbConsoleControl.isChecked()) tbConsoleControl.setChecked(false); if (layoutDial.getVisibility() != View.GONE) { Animation animation = new TranslateAnimation(0, 0, 0, 200 * metrics.density + 0.5f + displayHeight * 0.32f); animation.setDuration(200); layoutDial.startAnimation(animation); layoutDial.setVisibility(View.GONE); } if (containConsoleControl.getVisibility() != View.GONE) containConsoleControl.setVisibility(View.GONE); if (containViewPager.getVisibility() == View.GONE) containViewPager.setVisibility(View.VISIBLE); } else if (currentStatus == DIAL_STATUS) { if (!tbDial.isChecked()) tbDial.setChecked(true); if (tbConsoleControl.isChecked()) tbConsoleControl.setChecked(false); if (containConsoleControl.getVisibility() != View.GONE) containConsoleControl.setVisibility(View.GONE); if (containViewPager.getVisibility() != View.VISIBLE) containViewPager.setVisibility(View.VISIBLE); if (layoutDial.getVisibility() == View.GONE) { layoutDial.setVisibility(View.VISIBLE); Animation animation = new TranslateAnimation(0, 0, 200 * metrics.density + 0.5f + displayHeight * 0.32f, 0); animation.setDuration(300); layoutDial.startAnimation(animation); } sensorManager.unregisterListener(sensorListener, sensor); } else { sensorManager.registerListener(sensorListener, sensor, SensorManager.SENSOR_DELAY_UI); if (tbDial.isChecked()) tbDial.setChecked(false); if (!tbConsoleControl.isChecked()) tbConsoleControl.setChecked(true); if (layoutDial.getVisibility() != View.GONE) { Animation animation = new TranslateAnimation(0, 0, 0, 200 * metrics.density + 0.5f + displayHeight * 0.32f); animation.setDuration(200); layoutDial.startAnimation(animation); layoutDial.setVisibility(View.GONE); } if (containViewPager.getVisibility() != View.GONE) containViewPager.setVisibility(View.GONE); if (containConsoleControl.getVisibility() == View.GONE) containConsoleControl.setVisibility(View.VISIBLE); } } private void iniTaskStatus() { try { if (MyApplication.instance.taskStatusEntity != null) { String str = MyApplication.instance.taskStatusEntity.getRec_lvin_lvout(); int recode = Integer.parseInt(str.substring(0, 1)); if (recode == 0) { tbRecode.setChecked(false); } else tbRecode.setChecked(true); } } catch (Exception e) { ExceptionUtil.handlerException(e); } } private void iniCallStatus() { if (MyApplication.instance.currentCallStatusEntity.getL_cont() == 1) { tbCont.setChecked(true);//?????? } else tbCont.setChecked(false); if (MyApplication.instance.currentCallStatusEntity.getMic_mute() == 1) tbMute.setChecked(true);//??? else tbMute.setChecked(false); } private View.OnClickListener onClickListener = new View.OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.tabbed_it_hang_up: hangUp(); break; } } }; private void hangUp() { CallOutBiz.newInstance().hangUp(); } private void addListener() { mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { LogUtil.i(TAG, "PageScrolled" + position + " " + positionOffset + " " + positionOffsetPixels); } @Override public void onPageSelected(int position) { LogUtil.i(TAG, "PageSelected" + position + " "); } @Override public void onPageScrollStateChanged(int state) { LogUtil.i(TAG, "PageScrollStateChanged " + state); } }); ibHangUp.setOnClickListener(onClickListener); mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } private int preIndex = 0; @Override public void onPageSelected(int position) { Animation animation = new TranslateAnimation(preIndex * (displayWidth / 3), position * (displayWidth / 3), 0, 0); animation.setDuration(200); animation.setFillAfter(true); tvLine.startAnimation(animation); preIndex = position; } @Override public void onPageScrollStateChanged(int state) { } }); btnPanZoomOut.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: PanBiz.newInstance().movePan(getPtzcam_idx(), "zoom_out", -1); break; case MotionEvent.ACTION_UP: PanBiz.newInstance().movePan(getPtzcam_idx(), "stop", -1); break; } return true; } }); btnPanZoomIn.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: PanBiz.newInstance().movePan(getPtzcam_idx(), "zoom_in", -1); break; case MotionEvent.ACTION_UP: PanBiz.newInstance().movePan(getPtzcam_idx(), "stop", -1); break; } return true; } }); btnPanRight.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: PanBiz.newInstance().movePan(getPtzcam_idx(), "right", -1); break; case MotionEvent.ACTION_UP: PanBiz.newInstance().movePan(getPtzcam_idx(), "stop", -1); break; } return true; } }); btnPanLeft.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: PanBiz.newInstance().movePan(getPtzcam_idx(), "left", -1); break; case MotionEvent.ACTION_UP: PanBiz.newInstance().movePan(getPtzcam_idx(), "stop", -1); break; } return true; } }); btnPanUp.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: PanBiz.newInstance().movePan(getPtzcam_idx(), "up", -1); break; case MotionEvent.ACTION_UP: PanBiz.newInstance().movePan(getPtzcam_idx(), "stop", -1); break; } return true; } }); btnPanDown.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: PanBiz.newInstance().movePan(getPtzcam_idx(), "down", -1); LogUtil.i(TAG, "DOWN"); break; case MotionEvent.ACTION_UP: PanBiz.newInstance().movePan(getPtzcam_idx(), "stop", -1); LogUtil.i(TAG, "STOP"); break; } return true; } }); } private boolean isQuit = false; private Handler handler = new Handler(); @Override public void onBackPressed() { if (drawer.isDrawerVisible(navigationView)) { drawer.closeDrawers(); } else { if (isQuit) { super.onBackPressed(); for (Activity activity : MyApplication.instance.listActivity) { activity.finish(); } HaberManagerUtil.releaseResource(); } else { isQuit = true; Tools.showInfo(this, "?"); handler.postDelayed(new Runnable() { @Override public void run() { isQuit = false; } }, 3000); } } } private ActionBarDrawerToggle actionBarDrawerToggle; private void setViews() { toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); drawer = (DrawerLayout) findViewById(R.id.drawer_layout); actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.addDrawerListener(actionBarDrawerToggle); actionBarDrawerToggle.syncState(); actionBar = getSupportActionBar(); navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(new MyOnNavigationItemSelectedListener(this)); menuHeartLayout = (LinearLayout) navigationView.getHeaderView(0); tvSerialNo = (TextView) menuHeartLayout.findViewById(R.id.tv_menu_serial_no); iniLicenseInfo(); ibHangUp = (ImageButton) findViewById(R.id.tabbed_it_hang_up); tbCont = (ToggleButton) findViewById(R.id.tabbed_tb_cont); tbDial = (ToggleButton) findViewById(R.id.tabbed_tb_dial); tbMute = (ToggleButton) findViewById(R.id.tabbed_tb_mute); tbConsoleControl = (ToggleButton) findViewById(R.id.tabbed_tb_console_control); tbRecode = (ToggleButton) findViewById(R.id.tabbed_tb_recode); tbXX = (ToggleButton) findViewById(R.id.tabbed_tb_xx); layoutDial = (LinearLayout) findViewById(R.id.linearLayout_hangUp_dial); tvLine = findViewById(R.id.tv_Line_hangup);// containViewPager = (ConstraintLayout) findViewById(R.id.layout_contain_viewPager); containConsoleControl = (ConstraintLayout) findViewById(R.id.layout_hang_up_console_control_contain); btnPanDown = (ImageButton) findViewById(R.id.btn_hang_up_console_control_arrow_down); btnPanUp = (ImageButton) findViewById(R.id.btn_hang_up_console_control_arrow_up); btnPanLeft = (ImageButton) findViewById(R.id.btn_hang_up_console_control_arrow_left); btnPanRight = (ImageButton) findViewById(R.id.btn_hang_up_console_control_arrow_right); btnPanZoomIn = (ImageButton) findViewById(R.id.btn_hang_up_console_control_zoom_in); btnPanZoomOut = (ImageButton) findViewById(R.id.btn_hang_up_console_control_zoom_out); tvSwitcherPan = (TextView) findViewById(R.id.tv_console_control_hang_up_switcher); } private void iniLicenseInfo() { if (MyApplication.instance.licenseInfoEntity != null) { tvSerialNo.setText(MyApplication.instance.licenseInfoEntity.getSerial_no() + ""); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_tabbed, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); switch (id) { case R.id.action_settings: return true; } //noinspection SimplifiableIfStatement return super.onOptionsItemSelected(item); } public void onClick(View view) { try { switch (view.getId()) { case R.id.tabbed_tb_recode: handlerRecode(); break; case R.id.tabbed_tb_mute: handlerMicMute(); break; case R.id.tabbed_tb_cont: handerCont(); break; case R.id.tabbed_tb_xx: break; case R.id.tabbed_tb_console_control: handlerConsoleControl(); break; case R.id.tabbed_tb_dial: handlerDail(); break; case R.id.btn_hangup_dial_1: handlerDTMF(view); break; case R.id.btn_hangup_dial_2: handlerDTMF(view); break; case R.id.btn_hangup_dial_3: handlerDTMF(view); break; case R.id.btn_hangup_dial_4: handlerDTMF(view); break; case R.id.btn_hangup_dial_5: handlerDTMF(view); break; case R.id.btn_hangup_dial_6: handlerDTMF(view); break; case R.id.btn_hangup_dial_7: handlerDTMF(view); break; case R.id.btn_hangup_dial_8: handlerDTMF(view); break; case R.id.btn_hangup_dial_9: handlerDTMF(view); break; case R.id.btn_hangup_dial_0: handlerDTMF(view); break; case R.id.btn_hangup_dial_well: handlerDTMF(view); break; case R.id.btn_hangup_dial_asterisk: handlerDTMF(view); break; case R.id.tv_console_control_hang_up_hint: break; case R.id.tv_console_control_hang_up_switcher: switchPan(); break; } } catch (Exception e) { ExceptionUtil.handlerException(e); } } private void switchPan() { if (movePanRemote) { movePanRemote = false; tvSwitcherPan.setText(""); } else { movePanRemote = true; tvSwitcherPan.setText(""); } } private int getPtzcam_idx() { if (movePanRemote) return 100; else return 0; } private void handlerConsoleControl() { if (tbConsoleControl.isChecked()) { currentStatus = CONSOLE_CONTROL_STATUS; switchStatus(); } else { currentStatus = COMMON_STATUS; switchStatus(); } } private void handlerDTMF(View view) { Button btn = (Button) view; CallOutBiz.newInstance().sendDTMF(btn.getText().toString()); } private void handlerDail() { if (tbDial.isChecked()) { LogUtil.i(TAG, "tbDial.isChecked():" + tbDial.isChecked()); currentStatus = DIAL_STATUS; switchStatus(); } else { currentStatus = COMMON_STATUS; switchStatus(); } } @Override protected void onStart() { super.onStart(); try { registerReceiver(receiver, filter); handlerCallStatus(); iniTaskStatus(); iniSystemService(); switchStatus(); } catch (Exception e) { ExceptionUtil.handlerException(e); } } @Override protected void onStop() { super.onStop(); try { unregisterReceiver(receiver); } catch (Exception e) { ExceptionUtil.handlerException(e); } } private void handlerRecode() { if (tbRecode.isChecked()) { GeneralSettingBiz.newInstance().startRec();//? } else { GeneralSettingBiz.newInstance().stopRec();//? } } private void handlerMicMute() { if (tbMute.isChecked()) { GeneralSettingBiz.newInstance().muteMic();// } else { GeneralSettingBiz.newInstance().disMuteMic();// } } private void handerCont() { if (tbCont.isChecked()) { CallOutBiz.newInstance().startCont(); LogUtil.i(TAG, "????"); } else { CallOutBiz.newInstance().stopCont(); LogUtil.i(TAG, "???"); } } class MyBroadCaseReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { try { switch (intent.getAction()) { case Const.CALL_STATUS: handlerCallStatus(); break; case Const.ACTION_TASK_STATUS: handlerTaskStatus(); break; case Const.ACTION_UPDATA_LICENSE_INFO: iniLicenseInfo(); break; } } catch (Exception e) { ExceptionUtil.handlerException(e); } } } private void handlerTaskStatus() { iniTaskStatus(); LogUtil.i(TAG, ""); } private void handlerCallStatus() { if (MyApplication.instance.currentCallStatusEntity.getCall_stat() == 0) { Intent intent = new Intent(this, CallActivity.class); startActivity(intent); } iniCallStatus(); } }