package com.clj.blesample.tool.operation;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import com.clj.blesample.R;
import com.clj.blesample.tool.BluetoothService;
import java.util.ArrayList;
import java.util.List;
public class OperationActivity extends AppCompatActivity {
private Toolbar toolbar;
private List<Fragment> fragments = new ArrayList<>();
private int currentPage = 0;
private String[] titles = new String[]{"服务列表", "特征列表", "操作控制台"};
private BluetoothService mBluetoothService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_operation);
initView();
bindService();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mBluetoothService != null)
mBluetoothService.closeConnect();
unbindService();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (currentPage != 0) {
currentPage--;
changePage(currentPage);
return true;
} else {
finish();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
private void initView() {
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("特征列表");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (currentPage != 0) {
currentPage--;
changePage(currentPage);
} else {
finish();
}
}
});
}
private void initPage() {
prepareFragment();
changePage(0);
}
public void changePage(int page) {
currentPage = page;
toolbar.setTitle(titles[page]);
updateFragment(page);
if (currentPage == 1) {
((CharacteristicListFragment) fragments.get(1)).showData();
} else if (currentPage == 2) {
((CharacteristicOperationFragment) fragments.get(2)).showData();
}
}
private void prepareFragment() {
fragments.add(new ServiceListFragment());
fragments.add(new CharacteristicListFragment());
fragments.add(new CharacteristicOperationFragment());
for (Fragment fragment : fragments) {
getSupportFragmentManager().beginTransaction().add(R.id.fragment, fragment).hide(fragment).commit();
}
}
private void updateFragment(int position) {
if (position > fragments.size() - 1) {
return;
}
for (int i = 0; i < fragments.size(); i++) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Fragment fragment = fragments.get(i);
if (i == position) {
transaction.show(fragment);
} else {
transaction.hide(fragment);
}
transaction.commit();
}
}
public BluetoothService getBluetoothService() {
return mBluetoothService;
}
private void bindService() {
Intent bindIntent = new Intent(this, BluetoothService.class);
this.bindService(bindIntent, mFhrSCon, Context.BIND_AUTO_CREATE);
}
private void unbindService() {
this.unbindService(mFhrSCon);
}
private ServiceConnection mFhrSCon = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mBluetoothService = ((BluetoothService.BluetoothBinder) service).getService();
mBluetoothService.setConnectCallback(callback);
initPage();
}
@Override
public void onServiceDisconnected(ComponentName name) {
mBluetoothService = null;
}
};
private BluetoothService.Callback2 callback = new BluetoothService.Callback2() {
@Override
public void onDisConnected() {
finish();
}
};
}