package com.example.sensor_compass;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
/**
* 메인
* @author abyser
*
*/
public class Activity_Main extends Activity {
private static final String LOG_TAG = "Activity_Main";
private Button btn_show;
private LinearLayout linear_view;
/**
* Create activity
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
initButton();
}
private void initButton(){
btn_show = (Button)findViewById(R.id.btn_show);
btn_show.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Dialog_SensorList dialog = new Dialog_SensorList(Activity_Main.this);
dialog.show();
}
});
}
}
package com.example.sensor_compass;
import java.util.ArrayList;
import java.util.List;
import com.example.sensor_compass.adapter.Dialog_ListAdapter;
import com.example.sensor_compass.bean.Bean_SensorList;
import android.app.AlertDialog;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
/**
* 실제 리스트를 보여줄 다이얼로그
* @author abyser
*
*/
public class Dialog_SensorList {
// log tag
private static final String LOG_TAG = "Dialog_SensorList";
// constructor parameter
private Activity_Main activity;
// dialog
private AlertDialog mDialog;
private View dialog_view;
// layout
private TextView tv_number;
private ListView lv;
private Dialog_ListAdapter lv_adapter;
private ArrayList array_data;
/**
* Constructor
* @param activity
*/
public Dialog_SensorList(Activity_Main activity) {
this.activity = activity;
initView();
initDialog();
}
/**
* Create dialog view
*/
private void initView(){
dialog_view = activity.getLayoutInflater().inflate(R.layout.dialog_view, null);
tv_number = (TextView)dialog_view.findViewById(R.id.dialog_tv_number);
lv = (ListView)dialog_view.findViewById(R.id.dialog_lv);
array_data = new ArrayList();
// kind of sensor
// Sensor.TYPE_ALL - 모든 센서
// Sensor.TYPE_ACCELEROMETER - 가속 센서
// Sensor.TYPE_GRAVITY - 중력 센서
// Sensor.TYPE_GYROSCOPE - 회전 센서
// Sensor.TYPE_LIGHT - 조도 센서
// Sensor.TYPE_LINEAR_ACCELERATION - 선형 가속도 센서
// Sensor.TYPE_MAGNETIC_FIELD - 자기장 센서
// Sensor.TYPE_ORIENTATION - 방향 센서(SensorManager.GetOrientation()을 이용하여 방향값을 얻는게 권장 됨)
// Sensor.TYPE_PRESSURE - 압력 감지 센서
// Sensor.TYPE_PROXIMITY - 근접 센서
// Sensor.TYPE_ROTATION_VECTOR - 회전 센서
// Sensor.TYPE_TEMPERATURE - 온도 센서
SensorManager sensor_manager = (SensorManager)activity.getSystemService(Context.SENSOR_SERVICE);
List sensor_list = sensor_manager.getSensorList(Sensor.TYPE_ALL);
for(Sensor s : sensor_list){
Bean_SensorList bean = new Bean_SensorList();
bean.sensor_name = s.getName();
bean.sensor_type = String.valueOf(s.getType());
bean.sensor_power = String.valueOf(s.getPower()) + "mA";
bean.sensor_range = String.valueOf(s.getMaximumRange());
bean.sensor_vendor = s.getVendor();
bean.sensor_version = String.valueOf(s.getVersion());
bean.sensor_resolution = String.valueOf(s.getResolution());
array_data.add(bean);
}
lv_adapter = new Dialog_ListAdapter(activity, array_data);
lv.setAdapter(lv_adapter);
tv_number.setText("디바이스에 존재하는 센서의 수 : " + sensor_list.size());
}
/**
* Create dialog
*/
private void initDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setView(dialog_view);
builder.setTitle("센서 목록");
builder.setNegativeButton("확인", null);
mDialog = builder.create();
}
/**
* Show sensor list dialog
*/
public void show(){
if(activity == null){
Log.e(LOG_TAG, "activity is null");
return;
}
if(mDialog == null){
Log.e(LOG_TAG, "dialog is null");
return;
}
if(mDialog.isShowing()){
Log.w(LOG_TAG, "still show dialog");
return;
}
mDialog.show();
}
/**
* Dismiss sensor list dialog
*/
public void dismiss(){
if(!mDialog.isShowing()){
Log.e(LOG_TAG, "already dismiss dialog");
return;
}
mDialog.dismiss();
}
}
package com.example.sensor_compass.adapter;
import java.util.ArrayList;
import com.example.sensor_compass.Activity_Main;
import com.example.sensor_compass.R;
import com.example.sensor_compass.bean.Bean_SensorList;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
/**
* 리스트 어뎁터
* @author abyser
*
*/
public class Dialog_ListAdapter extends BaseAdapter{
private static final String LOG_TAG = "Dialog_ListAdapter";
private Activity_Main activity;
private ArrayList array_data;
/**
* Constructor
*/
public Dialog_ListAdapter(Activity_Main activity, ArrayList array) {
this.activity = activity;
this.array_data = array;
}
@Override
public int getCount() {
return array_data.size();
}
@Override
public Object getItem(int position) {
return array_data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
convertView = activity.getLayoutInflater().inflate(R.layout.dialog_list_item, null);
holder = new ViewHolder();
holder.tv_name = (TextView)convertView.findViewById(R.id.dialog_lv_item_tv_name);
holder.tv_type = (TextView)convertView.findViewById(R.id.dialog_lv_item_tv_type);
holder.tv_power = (TextView)convertView.findViewById(R.id.dialog_lv_item_tv_power);
holder.tv_range = (TextView)convertView.findViewById(R.id.dialog_lv_item_tv_range);
holder.tv_vendor = (TextView)convertView.findViewById(R.id.dialog_lv_item_tv_vendor);
holder.tv_version = (TextView)convertView.findViewById(R.id.dialog_lv_item_tv_version);
holder.tv_resolution = (TextView)convertView.findViewById(R.id.dialog_lv_item_tv_resolution);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.tv_name.setText("센서 이름 : " + array_data.get(position).sensor_name);
holder.tv_type.setText("센서 타입 : " + array_data.get(position).sensor_type);
holder.tv_power.setText("센서 파워 : " + array_data.get(position).sensor_power + "mA");
holder.tv_range.setText("센서 범위 : " + array_data.get(position).sensor_range);
holder.tv_vendor.setText("센서 제조사 : " + array_data.get(position).sensor_vendor);
holder.tv_version.setText("센서 버전 : " + array_data.get(position).sensor_version);
holder.tv_resolution.setText("센서 해상도 : " + array_data.get(position).sensor_resolution);
return convertView;
}
public static class ViewHolder {
private TextView tv_name; // 이름
private TextView tv_type; // 타입
private TextView tv_power; // 파워
private TextView tv_range; // 최대 측정 범위
private TextView tv_vendor; // 제조사
private TextView tv_version; // 버전
private TextView tv_resolution; // 해상도
}
}
package com.example.sensor_compass.bean;
/**
* * String getName () - 센서의 이름
* int getType () - 센서의 타입
* float getPower () - 전력 사용량을 밀리 암페어(mA)단위로 조사한다.
* float getMaximumRange () - 최대 측정 범위
* String getVendor () - 제조사
* int getVersion () - 버전
* float getResolution () - 해상도
*
*
* @author abyser
*
*/
public class Bean_SensorList {
public String sensor_name; // 센서 이름
public String sensor_type; // 센서 타입
public String sensor_power; // 센서 전력 사용량
public String sensor_range; // 센서 최대 측정 범위
public String sensor_vendor; // 센서 제조사
public String sensor_version; // 센서 버전
public String sensor_resolution; // 센서 해상도
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn_show"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="센서 목록" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/dialog_tv_number"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="지원하는 센서의 총 개수"
android:textSize="28dp"/>
<ListView
android:id="@+id/dialog_lv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/dialog_lv_item_tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="센서 이름(Name)" />
<TextView
android:id="@+id/dialog_lv_item_tv_type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="센서 타입(Type)" />
<TextView
android:id="@+id/dialog_lv_item_tv_power"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="센서 파워 정보(Power)mA" />
<TextView
android:id="@+id/dialog_lv_item_tv_range"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="최대 측정 범위(Range)" />
<TextView
android:id="@+id/dialog_lv_item_tv_vendor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="센서 제조사(Vendor)" />
<TextView
android:id="@+id/dialog_lv_item_tv_version"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="센서 버전(Version)" />
<TextView
android:id="@+id/dialog_lv_item_tv_resolution"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="센서 해상도(Resolution)" />
</LinearLayout>