Commit 54631d6c by onuma

#47941 HW連携 @FormからBluetoothのソースを移植 続き

parent 79136c8b
......@@ -183,4 +183,14 @@ public class Constant {
int CloseCameraActivity = 0;
int ThetaConnectError = 1;
}
// 連携機器の区分
public interface DeviceType {
int centerThermomete = 1; // 中心温度計
int sensor = 2; // 置くだけセンサー
int barcode = 3; // バーコード
int radiationThermomete = 4; // 放射温度計
int sppBluetoothMachine = 5; // SPP通信機器
int nfc = 6; // nfc機器
}
}
package jp.agentec.abook.abv.bl.dto;
/**
* bluetoothペアリング情報を扱うDto
* デバイス名、デバイスアドレス
*/
public class BluetoothPairingDeviceInfoDto {
/** デバイス名 */
public String deviceName;
/** デバイスアドレス*/
public String deviceAddress;
/** デバイスタイプ*/
public Integer deviceType;
}
......@@ -2,8 +2,10 @@ package jp.agentec.abook.abv.launcher.android;
import java.util.ArrayList;
import jp.agentec.abook.abv.bl.common.Constant;
import jp.agentec.abook.abv.bl.common.Constant.ReportType;
import jp.agentec.abook.abv.bl.common.log.Logger;
import jp.agentec.abook.abv.bl.dto.BluetoothPairingDeviceInfoDto;
import jp.agentec.abook.abv.bl.dto.OperationGroupMasterDto;
import jp.agentec.abook.abv.cl.util.PreferenceUtil;
import jp.agentec.abook.abv.ui.common.appinfo.AppDefType;
......@@ -179,4 +181,111 @@ public class ABVUIDataCache {
returnContentIdList.clear();
}
}
/**
* アプリ内で保存されてるbluetoothのペアリング情報の
* BluetoothPairingDeviceInfoDtoで返す。
*
* 情報が存在しない場合はnullで返す。
*
* @param bluetoothDeviceType 取得するbluetoothのデバイスタイプ
* @return BluetoothPairingDeviceInfoDto ペアリング情報
*/
public BluetoothPairingDeviceInfoDto getPairingBluetoothDeviceInfo(int bluetoothDeviceType) {
BluetoothPairingDeviceInfoDto dto = new BluetoothPairingDeviceInfoDto();
String deviceName = null;
String deviceAddress = null;
if (bluetoothDeviceType == Constant.DeviceType.centerThermomete) {
// 中心温度計
deviceName = PreferenceUtil.getUserPref(context, UserPrefKey.BLE_DEVICE_CENTER_TEMPERATURE_NAME, "");
deviceAddress = PreferenceUtil.getUserPref(context, UserPrefKey.BLE_DEVICE_CENTER_TEMPERATURE_ADDRESS, "");
} else if (bluetoothDeviceType == Constant.DeviceType.radiationThermomete) {
// 放射温度計
deviceName = PreferenceUtil.getUserPref(context, UserPrefKey.BLE_DEVICE_RADIATION_TEMPERATURE_NAME, "");
deviceAddress = PreferenceUtil.getUserPref(context, UserPrefKey.BLE_DEVICE_RADIATION_TEMPERATURE_ADDRESS, "");
}
// deviceNameとdeviceAddressがセットなので、どっちかの値が存在しないとnullで返す。
if (StringUtil.isNullOrEmpty(deviceName) || StringUtil.isNullOrEmpty(deviceAddress)) {
return null;
}
dto.deviceName = deviceName;
dto.deviceAddress = deviceAddress;
dto.deviceType = bluetoothDeviceType;
return dto;
}
/**
* 引数のデバイスタイプでローカルに保存されてるbluetoothのデバイスアドレス取得
* @param bluetoothDeviceType
* @return
*/
public String getPairingBluetoothDeviceAddress(int bluetoothDeviceType) {
String deviceAddressKey = null;
if (bluetoothDeviceType == Constant.DeviceType.centerThermomete) {
deviceAddressKey = UserPrefKey.BLE_DEVICE_CENTER_TEMPERATURE_ADDRESS;
} else if (bluetoothDeviceType == Constant.DeviceType.radiationThermomete) {
deviceAddressKey = UserPrefKey.BLE_DEVICE_RADIATION_TEMPERATURE_ADDRESS;
}
if (deviceAddressKey == null) {
// 引数のデバイスタイプが定義した以外の値が入った場合
return null;
}
return PreferenceUtil.getUserPref(context, deviceAddressKey, "");
}
/**
* 引数であるデバイスタイプリストに対するアプリ内で保存されてる
* bluetoothのペアリング情報リストを返す。
* @param deviceTypeList
* @return List<BluetoothPairingDeviceInfoDto> ペアリング情報リスト
*/
public List<BluetoothPairingDeviceInfoDto> getPairingBluetoothDeviceInfoList(List<Integer> deviceTypeList) {
List<BluetoothPairingDeviceInfoDto> bluetoothPairingDeviceInfoDtoList = new ArrayList<BluetoothPairingDeviceInfoDto>();
for (Integer deviceType : deviceTypeList) {
// 引数のデバイスタイプのリスト全部を1つずつデータ取得
BluetoothPairingDeviceInfoDto dto = getPairingBluetoothDeviceInfo(deviceType);
if (dto != null) {
// dtoがnullではない場合のみリストに追加
bluetoothPairingDeviceInfoDtoList.add(dto);
}
}
return bluetoothPairingDeviceInfoDtoList;
}
/**
* ペアリング情報をローカルに保存する
* deviceType毎に保存するキーが異なる
* @param pairingDeviceInfoDto
*/
public void setPairingBluetoothDeviceInfo(BluetoothPairingDeviceInfoDto pairingDeviceInfoDto) {
if (pairingDeviceInfoDto.deviceType.equals(Constant.DeviceType.centerThermomete)) {
// 中心温度計
PreferenceUtil.putUserPref(context, UserPrefKey.BLE_DEVICE_CENTER_TEMPERATURE_NAME, pairingDeviceInfoDto.deviceName);
PreferenceUtil.putUserPref(context, UserPrefKey.BLE_DEVICE_CENTER_TEMPERATURE_ADDRESS, pairingDeviceInfoDto.deviceAddress);
} else if (pairingDeviceInfoDto.deviceType.equals(Constant.DeviceType.radiationThermomete)) {
// 放射温度計
PreferenceUtil.putUserPref(context, UserPrefKey.BLE_DEVICE_RADIATION_TEMPERATURE_NAME, pairingDeviceInfoDto.deviceName);
PreferenceUtil.putUserPref(context, UserPrefKey.BLE_DEVICE_RADIATION_TEMPERATURE_ADDRESS, pairingDeviceInfoDto.deviceAddress);
}
}
/**
* 引数であるデバイスタイプの
* ペアリング情報を削除する
* @param deviceType
*/
public void removePairingBluetoothDeviceInfo(int deviceType) {
if (deviceType == Constant.DeviceType.centerThermomete) {
// 中心温度計
PreferenceUtil.removeUserPref(context, UserPrefKey.BLE_DEVICE_CENTER_TEMPERATURE_NAME);
PreferenceUtil.removeUserPref(context, UserPrefKey.BLE_DEVICE_CENTER_TEMPERATURE_ADDRESS);
} else if (deviceType == Constant.DeviceType.radiationThermomete) {
// 放射温度計
PreferenceUtil.removeUserPref(context, UserPrefKey.BLE_DEVICE_RADIATION_TEMPERATURE_NAME);
PreferenceUtil.removeUserPref(context, UserPrefKey.BLE_DEVICE_RADIATION_TEMPERATURE_ADDRESS);
}
}
}
......@@ -65,6 +65,14 @@ public interface AppDefType {
String RESOURCE_PATTERN_TYPE = "resourcePatternType"; // 文言リソースパターン
// 中心温度計
String BLE_DEVICE_CENTER_TEMPERATURE_NAME = "bleDeviceCenterTemperatureName"; // 中心温度計機器の名
String BLE_DEVICE_CENTER_TEMPERATURE_ADDRESS = "bleDeviceCenterTemperatureAddress"; // 中心温度計機器のアドレス
// 放射温度計
String BLE_DEVICE_RADIATION_TEMPERATURE_NAME = "bleDeviceRadiationTemperatureName"; // 放射温度計機器の名
String BLE_DEVICE_RADIATION_TEMPERATURE_ADDRESS = "bleDeviceRadiationTemperatureAddress"; // 放射温度計機器のアドレス
String OPERATION_GROUP_MASERT_MODE = "operation_group_master"; // 通常・作業種別モード(画面)
String OPERATION_GROUP_MASERT_ID = "operation_group_master_id"; // 作業種別のID
......
......@@ -24,11 +24,16 @@ import java.util.Arrays;
import java.util.List;
import jp.agentec.abook.abv.bl.common.Constant;
import jp.agentec.abook.abv.bl.common.Constant.DeviceType;
import jp.agentec.abook.abv.bl.common.log.Logger;
import jp.agentec.abook.abv.bl.dto.BluetoothPairingDeviceInfoDto;
import jp.agentec.abook.abv.cl.util.BleManagerUtil;
import jp.agentec.abook.abv.launcher.android.R;
import jp.agentec.abook.abv.ui.common.activity.ABVUIActivity;
import jp.agentec.abook.abv.ui.common.util.ABVToastUtil;
import jp.agentec.abook.abv.ui.home.adapter.BleListAdapter;
import jp.agentec.abook.abv.ui.home.adapter.BleListRowData;
import jp.agentec.abook.abv.ui.home.adapter.common.SectionHeaderData;
import jp.agentec.abook.abv.ui.home.helper.ABookPermissionHelper;
import jp.agentec.adf.util.CollectionUtil;
......@@ -47,7 +52,7 @@ public class BlePairingSettingActivity extends ABVUIActivity {
private boolean mScanning = false; // スキャン中かどうかのフラグ
private Button mButton_Scan;
private BleManagerUtil bleManagerUtil;
private BleListAdapter mBleListAdapter; // Adapter
private BleListAdapter mBleListAdapter; // Adapter
private List<BluetoothDevice> mScanDeviceInfoList = new ArrayList<BluetoothDevice>();
private List<String> mSavedDeviceAddressList = new ArrayList<String>(); //登録した端末アドレス
......@@ -98,10 +103,10 @@ public class BlePairingSettingActivity extends ABVUIActivity {
protected void onCreate(Bundle savedInstanceState) {
Logger.i(TAG, "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.pairing_setting);
//setContentView(R.layout.pairing_setting);
TextView deviceTitle = (TextView) findViewById(R.id.device_toolbar_title);
deviceTitle.setText(getString(R.string.chino_machine));
//TextView deviceTitle = (TextView) findViewById(R.id.device_toolbar_title);
//eviceTitle.setText(getString(R.string.chino_machine));
// 戻り値の初期化
setResult( Activity.RESULT_CANCELED );
......@@ -140,25 +145,25 @@ public class BlePairingSettingActivity extends ABVUIActivity {
// bleManagerUtil.mBluetoothAdapter.startDiscovery();
ListView listView = (ListView) findViewById(R.id.devicelist); // リストビューの取得
listView.setAdapter(mBleListAdapter); // リストビューにビューアダプターをセット
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Logger.d(TAG, "position = " + position);
BleListRowData bleListRowData = (BleListRowData)parent.getItemAtPosition(position);
// 既に保存されてる場合は何もしない
if (!bleListRowData.isSaved) {
localSaveDeviceInfo(bleListRowData);
}
}
});
// ListView listView = (ListView) findViewById(R.id.devicelist); // リストビューの取得
// listView.setAdapter(mBleListAdapter); // リストビューにビューアダプターをセット
// listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
// @Override
// public void onItemClick(AdapterView<?> parent, View view, int position,
// long id) {
// Logger.d(TAG, "position = " + position);
// BleListRowData bleListRowData = (BleListRowData)parent.getItemAtPosition(position);
// // 既に保存されてる場合は何もしない
// if (!bleListRowData.isSaved) {
// localSaveDeviceInfo(bleListRowData);
// }
// }
// });
// Reload Button
mButton_Scan = (Button)findViewById( R.id.btn_reload );
//mButton_Scan = (Button)findViewById( R.id.btn_reload );
mButton_Scan.setAllCaps(false);
mButton_Scan.setText(getString(R.string.pairing_search_scan));
//mButton_Scan.setText(getString(R.string.pairing_search_scan));
mButton_Scan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
......@@ -198,7 +203,7 @@ public class BlePairingSettingActivity extends ABVUIActivity {
switch (requestCode) {
case REQUEST_ENABLEBLUETOOTH: // Bluetooth有効化要求
if( Activity.RESULT_CANCELED == resultCode ) { // 有効にされなかった
ABVToastUtil.showMakeText(getApplicationContext(), String.format(getString(R.string.msg_scan_bluetooth_no_allow), getString(R.string.chino_machine)), Toast.LENGTH_SHORT);
//ABVToastUtil.showMakeText(getApplicationContext(), String.format(getString(R.string.msg_scan_bluetooth_no_allow), getString(R.string.chino_machine)), Toast.LENGTH_SHORT);
return;
}
break;
......@@ -224,7 +229,7 @@ public class BlePairingSettingActivity extends ABVUIActivity {
//端末側の位置情報許可チェック
if (!(gpsEnabled || secureLocationGpsEnabled)) {
showSimpleAlertDialog(R.string.chino_machine, R.string.msg_location_device_no_allow);
//showSimpleAlertDialog(R.string.chino_machine, R.string.msg_location_device_no_allow);
return;
}
......@@ -259,7 +264,7 @@ public class BlePairingSettingActivity extends ABVUIActivity {
mScanning = true;
mButton_Scan.setText(getString(R.string.pairing_search_stop));
//Button_Scan.setText(getString(R.string.pairing_search_stop));
reloadListView();
Logger.d(TAG, "start scan !!");
}
......@@ -282,7 +287,7 @@ public class BlePairingSettingActivity extends ABVUIActivity {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
scanner.stopScan( mLeScanCallback );
}
mButton_Scan.setText(getString(R.string.pairing_search_scan));
//mButton_Scan.setText(getString(R.string.pairing_search_scan));
reloadListView();
Logger.d(TAG, "stop scan !!");
}
......@@ -339,14 +344,14 @@ public class BlePairingSettingActivity extends ABVUIActivity {
for (BluetoothPairingDeviceInfoDto bluetoothPairingDeviceInfoDto : bluetoothPairingInfoDtoList) {
// ペアリング情報が既に保存されてる場合はヘッダー情報を各機器毎に追加する
if (bluetoothPairingDeviceInfoDto.deviceType.equals(DeviceType.centerThermomete)) {
sectionList.add(new SectionHeaderData(String.format(getString(R.string.pairing_save_machine), getString(R.string.center_thermometer))));
//sectionList.add(new SectionHeaderData(String.format(getString(R.string.pairing_save_machine), getString(R.string.center_thermometer))));
} else if (bluetoothPairingDeviceInfoDto.deviceType.equals(DeviceType.radiationThermomete)) {
sectionList.add(new SectionHeaderData(String.format(getString(R.string.pairing_save_machine), getString(R.string.radiation_thermometer))));
//sectionList.add(new SectionHeaderData(String.format(getString(R.string.pairing_save_machine), getString(R.string.radiation_thermometer))));
}
}
}
// その他のヘッダー情報追加
sectionList.add(new SectionHeaderData(String.format(getString(mScanning ? R.string.pairing_other_machine_searching : R.string.pairing_other_machine), getString(R.string.chino_machine))));
//sectionList.add(new SectionHeaderData(String.format(getString(mScanning ? R.string.pairing_other_machine_searching : R.string.pairing_other_machine), getString(R.string.chino_machine))));
return sectionList;
}
......@@ -384,9 +389,9 @@ public class BlePairingSettingActivity extends ABVUIActivity {
for (BluetoothDevice bleDevice : mScanDeviceInfoList) {
String labelDeviceName = "";
if (bleDevice.getName().startsWith(CENTER_THERMOMETE_DEVICE_NAME)) {
labelDeviceName = getString(R.string.center_thermometer);
//labelDeviceName = getString(R.string.center_thermometer);
} else if (bleDevice.getName().startsWith(RADIATION_THERMOMETE_DEVICE_NAME)) {
labelDeviceName = getString(R.string.radiation_thermometer);
//labelDeviceName = getString(R.string.radiation_thermometer);
}
BleListRowData scanRowData = new BleListRowData(bleDevice.getName(), labelDeviceName, bleDevice.getAddress(), false);
scanRowDataList.add(scanRowData);
......
......@@ -8,95 +8,95 @@ import android.widget.TextView;
import java.util.List;
import jp.agentec.abook.abv.launcher.android.R;
import jp.agentec.abook.abv.ui.home.adapter.common.BaseSectionAdapter;
import jp.agentec.abook.abv.ui.home.adapter.common.IndexPath;
import jp.agentec.abook.abv.ui.home.adapter.common.SectionHeaderData;
public class BleListAdapter {
public class BleListAdapter extends BaseSectionAdapter<SectionHeaderData, BleListRowData> {
private static final String TAG = "BleListAdapter";
protected jp.agentec.abook.abv.ui.home.adapter.BleListAdapter.BleListAdapterListener listener;
public class BleListAdapter extends BaseSectionAdapter<SectionHeaderData, BleListRowData> {
private static final String TAG = "BleListAdapter";
protected BleListAdapter.BleListAdapterListener listener;
public interface BleListAdapterListener {
// 登録されたデバイス情報削除(bluetooth情報を引数としてセット)
void onDeleteConnectInfo(BleListRowData rowData);
}
public interface BleListAdapterListener {
// 登録されたデバイス情報削除(bluetooth情報を引数としてセット)
void onDeleteConnectInfo(BleListRowData rowData);
}
public BleListAdapter(Context context, List<SectionHeaderData> sectionList,
List<List<BleListRowData>> rowList, BleListAdapterListener listener) {
super(context, sectionList, rowList);
this.listener = listener;
}
public BleListAdapter(Context context, List<SectionHeaderData> sectionList,
List<List<BleListRowData>> rowList, BleListAdapterListener listener) {
super(context, sectionList, rowList);
this.listener = listener;
}
@Override
public View viewForHeaderInSection(View convertView, int section) {
ListHeaderViewHolder holder = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.ble_section_list_header, null);
holder = new ListHeaderViewHolder();
holder.titleTxt = (TextView) convertView.findViewById(R.id.titleTxt);
holder.subtitleTxt = (TextView) convertView.findViewById(R.id.subTitleTxt);
convertView.setTag(holder);
} else {
holder = (ListHeaderViewHolder) convertView.getTag();
}
SectionHeaderData headerData = sectionList.get(section);
holder.titleTxt.setText(headerData.title);
holder.subtitleTxt.setText(headerData.subTitle);
return convertView;
@Override
public View viewForHeaderInSection(View convertView, int section) {
ListHeaderViewHolder holder = null;
if (convertView == null) {
//convertView = inflater.inflate(R.layout.ble_section_list_header, null);
holder = new ListHeaderViewHolder();
//holder.titleTxt = (TextView) convertView.findViewById(R.id.titleTxt);
// holder.subtitleTxt = (TextView) convertView.findViewById(R.id.subTitleTxt);
convertView.setTag(holder);
} else {
holder = (ListHeaderViewHolder) convertView.getTag();
}
SectionHeaderData headerData = sectionList.get(section);
holder.titleTxt.setText(headerData.title);
holder.subtitleTxt.setText(headerData.subTitle);
return convertView;
}
@Override
public View cellForRowAtIndexPath(View convertView, IndexPath indexPath) {
ListRowViewHolder holder = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.ble_section_list_row, null);
holder = new ListRowViewHolder();
// bluetoothのデバイス名
holder.bl_title = (TextView) convertView.findViewById(R.id.bl_title);
// 該当通信機器の名(中心温度計・放射温度計)
holder.sub_title = (TextView) convertView.findViewById(R.id.sub_title);
// 削除ボタン
holder.bl_deleteBtn = (Button) convertView.findViewById(R.id.bl_deleteBtn);
convertView.setTag(holder);
} else {
holder = (ListRowViewHolder) convertView.getTag();
}
final BleListRowData rowData = rowList.get(indexPath.section).get(indexPath.row);
holder.bl_title.setText(rowData.title);
if (rowData.isSaved) {
// 既に保存されてる場合、削除ボタン表示・機器名は非表示
holder.sub_title.setVisibility(View.GONE);
holder.bl_deleteBtn.setVisibility(View.VISIBLE);
holder.bl_deleteBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onDeleteConnectInfo(rowData);
}
});
} else {
// スキャンされたbluetooth機器表示時に削除ボタンと接続ステータス非表示
holder.bl_deleteBtn.setVisibility(View.GONE);
holder.sub_title.setVisibility(View.VISIBLE);
holder.sub_title.setText(rowData.subTitle);
}
return convertView;
@Override
public View cellForRowAtIndexPath(View convertView, IndexPath indexPath) {
ListRowViewHolder holder = null;
if (convertView == null) {
//convertView = inflater.inflate(R.layout.ble_section_list_row, null);
holder = new ListRowViewHolder();
// bluetoothのデバイス名
//holder.bl_title = (TextView) convertView.findViewById(R.id.bl_title);
// 該当通信機器の名(中心温度計・放射温度計)
//holder.sub_title = (TextView) convertView.findViewById(R.id.sub_title);
// 削除ボタン
//holder.bl_deleteBtn = (Button) convertView.findViewById(R.id.bl_deleteBtn);
convertView.setTag(holder);
} else {
holder = (ListRowViewHolder) convertView.getTag();
}
final BleListRowData rowData = rowList.get(indexPath.section).get(indexPath.row);
holder.bl_title.setText(rowData.title);
static class ListHeaderViewHolder {
TextView titleTxt;
TextView subtitleTxt;
if (rowData.isSaved) {
// 既に保存されてる場合、削除ボタン表示・機器名は非表示
holder.sub_title.setVisibility(View.GONE);
holder.bl_deleteBtn.setVisibility(View.VISIBLE);
holder.bl_deleteBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onDeleteConnectInfo(rowData);
}
});
} else {
// スキャンされたbluetooth機器表示時に削除ボタンと接続ステータス非表示
holder.bl_deleteBtn.setVisibility(View.GONE);
holder.sub_title.setVisibility(View.VISIBLE);
holder.sub_title.setText(rowData.subTitle);
}
return convertView;
}
static class ListRowViewHolder {
TextView bl_title;
TextView sub_title;
Button bl_deleteBtn;
}
static class ListHeaderViewHolder {
TextView titleTxt;
TextView subtitleTxt;
}
public void setItem(List<SectionHeaderData> sectionList, List<List<BleListRowData>> rowList) {
settingSectionRowData(sectionList, rowList);
notifyDataSetChanged();
}
static class ListRowViewHolder {
TextView bl_title;
TextView sub_title;
Button bl_deleteBtn;
}
public void setItem(List<SectionHeaderData> sectionList, List<List<BleListRowData>> rowList) {
settingSectionRowData(sectionList, rowList);
notifyDataSetChanged();
}
}
package jp.agentec.abook.abv.ui.home.adapter;
import jp.agentec.abook.abv.ui.home.adapter.common.SectionRowData;
public class BleListRowData extends SectionRowData {
public boolean isSaved;
public String deviceAddress;
/**
* デフォルト
* @param title タイトル
* @param subTitle サブタイトル
*/
public BleListRowData(String title, String subTitle) {
super(title, subTitle);
}
/**
* bluetooth情報
* @param title bluetoothデバイス名
* @param deviceAddress bluetoothデバイスアドレス
* @param isSaved 登録状態
*/
public BleListRowData(String title, String deviceAddress, boolean isSaved) {
super(title, null);
this.deviceAddress = deviceAddress;
this.isSaved = isSaved;
}
/**
* bluetooth情報
* @param title bluetoothデバイス名
* @param subTitle 通信機器名(中心温度計・放射温度計)
* @param deviceAddress bluetoothデバイスアドレス
* @param isSaved 登録状態
*/
public BleListRowData(String title, String subTitle, String deviceAddress, boolean isSaved) {
super(title, subTitle);
this.deviceAddress = deviceAddress;
this.isSaved = isSaved;
}
}
package jp.agentec.abook.abv.ui.home.adapter.common;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class BaseSectionAdapter<T1, T2> extends BaseAdapter {
/** インデックス行:ヘッダー */
private static final int INDEX_PATH_ROW_HEADER = -1;
/** ビュータイプ:ヘッダー行 */
private static final int ITEM_VIEW_TYPE_HEADER = 0;
/** ビュータイプ:データ行 */
private static final int ITEM_VIEW_TYPE_ROW = 1;
protected Context context;
protected LayoutInflater inflater;
/** ヘッダー行で使用するデータリスト */
protected List<T1> sectionList;
/** データ行で使用するデータリスト */
protected List<List<T2>> rowList;
private List<IndexPath> indexPathList;
public BaseSectionAdapter(Context context, List<T1> sectionList, List<List<T2>> rowList) {
super();
this.context = context;
this.inflater = LayoutInflater.from(context);
this.sectionList = sectionList;
this.rowList = rowList;
this.indexPathList = getIndexPathList(sectionList, rowList);
}
@Override
public int getCount() {
int count = indexPathList.size();
return count;
}
@Override
public Object getItem(int position) {
IndexPath indexPath = indexPathList.get(position);
if (isHeader(indexPath)) {
return sectionList.get(indexPath.section);
} else {
return rowList.get(indexPath.section).get(indexPath.row);
}
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
IndexPath indexPath = indexPathList.get(position);
// ヘッダー行とデータ行とで分岐します。
if (isHeader(indexPath)) {
return viewForHeaderInSection(convertView, indexPath.section);
} else {
return cellForRowAtIndexPath(convertView, indexPath);
}
}
/**
* ヘッダー行のViewを返します。
*
* @param convertView
* @param section
* @return ヘッダー行のView
*/
public View viewForHeaderInSection(View convertView, int section) {
if (convertView == null) {
convertView = inflater.inflate(android.R.layout.simple_list_item_1, null);
TextView castedConvertView = (TextView) convertView;
castedConvertView.setBackgroundColor(Color.GRAY);
castedConvertView.setTextColor(Color.WHITE);
}
TextView textView = (TextView) convertView;
textView.setText(sectionList.get(section).toString());
return convertView;
}
/**
* データ行のViewを返します。
*
* @param convertView
* @param indexPath
* @return データ行のView
*/
public View cellForRowAtIndexPath(View convertView, IndexPath indexPath) {
if (convertView == null) {
convertView = inflater.inflate(android.R.layout.simple_list_item_1, null);
}
TextView textView = (TextView) convertView;
textView.setText(rowList.get(indexPath.section).get(indexPath.row).toString());
return convertView;
}
@Override
public int getViewTypeCount() {
// ヘッダー行とデータ行の2種類なので、2を返します。
return 2;
}
@Override
public int getItemViewType(int position) {
// ビュータイプを返します。
if (isHeader(position)) {
return ITEM_VIEW_TYPE_HEADER;
} else {
return ITEM_VIEW_TYPE_ROW;
}
}
@Override
public boolean isEnabled(int position) {
if (isHeader(position)) {
// ヘッダー行の場合は、タップできないようにします。
return false;
} else {
IndexPath indexPath = indexPathList.get(position);
SectionRowData rowData = (SectionRowData) rowList.get(indexPath.section).get(indexPath.row);
if (rowData.title.isEmpty()) { //タイトルが空白の場合はタップ不能に設定
return false;
}
return super.isEnabled(position);
}
}
/**
* インデックスパスリストを取得します。
*
* @param sectionList
* @param rowList
* @return インデックスパスリスト
*/
private List<IndexPath> getIndexPathList(List<T1> sectionList, List<List<T2>> rowList) {
List<IndexPath> indexPathList = new ArrayList<IndexPath>();
for (int i = 0; i < sectionList.size(); i++) {
IndexPath sectionIndexPath = new IndexPath();
sectionIndexPath.section = i;
sectionIndexPath.row = INDEX_PATH_ROW_HEADER;
indexPathList.add(sectionIndexPath);
List<T2> rowListBySection = rowList.get(i);
for (int j = 0; j < rowListBySection.size(); j++) {
IndexPath rowIndexPath = new IndexPath();
rowIndexPath.section = i;
rowIndexPath.row = j;
indexPathList.add(rowIndexPath);
}
}
return indexPathList;
}
private boolean isHeader(int position) {
IndexPath indexPath = indexPathList.get(position);
return isHeader(indexPath);
}
private boolean isHeader(IndexPath indexPath) {
if (INDEX_PATH_ROW_HEADER == indexPath.row) {
return true;
} else {
return false;
}
}
protected void settingSectionRowData(List<T1> sectionList, List<List<T2>> rowList) {
this.sectionList = sectionList;
this.rowList = rowList;
this.indexPathList = getIndexPathList(sectionList, rowList);
}
}
package jp.agentec.abook.abv.ui.home.adapter.common;
public class IndexPath {
public int section;
public int row;
}
package jp.agentec.abook.abv.ui.home.adapter.common;
/**
* ListViewのAdapterで利用するSectionの値設定クラス
*/
public class SectionHeaderData {
/**
* 中心温度計用
* @param title タイトル
*/
public SectionHeaderData(String title) {
this.title = title;
}
/**
* 共通用
* @param title タイトル
* @param subTitle サブタイトル
*/
public SectionHeaderData(String title, String subTitle) {
this.title = title;
this.subTitle = subTitle;
}
public String title;
public String subTitle;
}
\ No newline at end of file
package jp.agentec.abook.abv.ui.home.adapter.common;
/**
* ListViewのAdapterで利用するRowの値設定クラス
*/
public class SectionRowData {
/**
* 共通用
* @param title タイトル
* @param subTitle サブタイトル
*/
public SectionRowData(String title, String subTitle) {
this.title = title;
this.subTitle = subTitle;
}
public String title;
public String subTitle;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment