package lee.hyeontae.C25_Camera;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import android.app.Activity;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.PictureCallback;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class C25_Camera extends Activity {
SharedPreferences preferences;
SharedPreferences.Editor editor;
MyCameraSuface mSurface;
ImageButton mShutter;
TextView providerTextView, locationTextView, currentDestination;
public static LocationManager LM;
String mBestProvider;
int mCount=0;
public static PendingIntent mPending;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
InitViews();
preferences =
getSharedPreferences("destination", Service.MODE_PRIVATE);
LocationProviderList();
locationAlert();
/**
* @description 카메라 촬영 버튼을 눌렀을 경우 동작.
*/
mShutter.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
mShutter.setEnabled(false);
mSurface.mCamera.autoFocus(mAutoFocus);
handler.sendEmptyMessageDelayed(0, 1000);
}
});
}
/**
* @description 카메라 촬영시 Focus조정후 1초뒤 자동으로 촬영함.
*/
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
mSurface.mCamera.takePicture(null, null, mPicture);
}
};
/**
* @description Focus조정을 위한 Callback
*/
AutoFocusCallback mAutoFocus = new AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
mShutter.setEnabled(success);
}
};
/**
* @description 카메라 영상을 JPG파일로 저장.
*/
PictureCallback mPicture = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data,
Camera camera) {
String sd = Environment.getExternalStorageDirectory()
.getAbsolutePath();
String path = sd + "/cameratest.jpg";
File file = new File(path);
try {
FileOutputStream fos
= new FileOutputStream(file);
fos.write(data);
fos.flush();
fos.close();
} catch (Exception e) {
Toast.makeText(C25_Camera.this, "파일 저장 중 에러 발생 : " + e.getMessage(), 0).show();
return;
}
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri uri = Uri.parse("file://" + path);
intent.setData(uri);
sendBroadcast(intent);
Toast.makeText(C25_Camera.this, "사진 저장 완료 : " + path, 0).show();
mSurface.mCamera.startPreview();
}
};
/**
* @description 위치정보를 구하기에 최적의 Provider를 선택.
*/
@SuppressWarnings("static-access")
private void LocationProviderList() {
LM =
(LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
List<String> arProvider = LM.getProviders(false);
String result = "";
for (int i = 0; i < arProvider.size(); i++) {
result += ("Provider " + i + " : " +
arProvider.get(i) + "\n");
}
Criteria crit = new Criteria();
crit.setAccuracy(Criteria.NO_REQUIREMENT);
crit.setPowerRequirement(Criteria.NO_REQUIREMENT);
crit.setAltitudeRequired(false);
crit.setCostAllowed(false);
mBestProvider = LM.getBestProvider(crit, true);
result += ("\nBest Provider : " + mBestProvider + "\n\n");
result += LM.GPS_PROVIDER + " :
" + LM.isProviderEnabled(LocationManager.GPS_PROVIDER) + "\n";
result += LM.NETWORK_PROVIDER + " :
" + LM.isProviderEnabled(LocationManager.NETWORK_PROVIDER) + "\n";
providerTextView.setText(result);
}
/**
* @description 모든 View를 초기화함.
*/
private void InitViews() {
mSurface =
(MyCameraSuface) findViewById(R.id.preview);
mShutter =
(ImageButton) findViewById(R.id.shutter);
Drawable alpha1 = mShutter.getBackground();
alpha1.setAlpha(0);
providerTextView =
(TextView) findViewById(R.id.providerTextView);
locationTextView =
(TextView) findViewById(R.id.currentLocationTextView);
currentDestination = (TextView)findViewById(R.id.currentDestinationTextView);
}
/**
* @description 단말의 현재 위치를 구함.
*/
LocationListener mListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
mCount++;
String sloc = String.format("수신회수:%d\n위도:%f\n경도:%f\n고도:%f", mCount,
location.getLatitude(),
location.getLongitude(), location.getAltitude());
locationTextView.setText(sloc);
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
/**
* @description 특정 지역 접근시 알림 팝업 출력. 예) 가산 디지털 단지역
*/
private void locationAlert() {
Log.d("dodo4989","locationAlert");
Intent intent = new Intent(this, LocationAlertReceiver.class);
intent.setAction("lee.hyeontae.C25_Camera");
mPending =
PendingIntent.getBroadcast(this, 0, intent, 0);
Log.d("dodo4989","preferences.getInt="+preferences.getInt("where", 4));
switch (preferences.getInt("where", 4)){
case 1:
LM.addProximityAlert(37.48030511658541,
126.8828335404396, 500, -1, mPending);
currentDestination.setText("가산디지털단지역");
break;
case 2:
LM.addProximityAlert(37.4994995976215,
126.9287234544754, 500, -1, mPending);
currentDestination.setText("신대방삼거리역");
break;
case 3:
LM.addProximityAlert(37.483957534386015,
126.93013697862625, 500, -1, mPending);
currentDestination.setText("신림역");
break;
case 4:
LM.addProximityAlert(37.487049,
126.878930, 500, -1, mPending);
currentDestination.setText("회사");
break;
}
}
/**
* @description 메뉴 버튼 Create
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
SubMenu sub = menu.addSubMenu("목적지설정");
sub.add(0,1,0,"가산디지털단지역");
sub.add(0,2,0,"신대방삼거리역");
sub.add(0,3,0,"신림역");
sub.add(0,4,0,"회사");
return super.onCreateOptionsMenu(menu);
}
/**
* @description 메뉴에서 각 옵션 선택시 동작 구현.
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
editor = preferences.edit();
Intent intent = new Intent(this, LocationAlertReceiver.class);
intent.setAction("lee.hyeontae.C25_Camera");
mPending =
PendingIntent.getBroadcast(this, 0, intent, 0);
switch(item.getItemId()){
case 1:
Log.d("dodo4989","1");
editor.putInt("where", 1);
LM.addProximityAlert(37.48030511658541,
126.8828335404396, 500, -1, mPending);
currentDestination.setText("가산디지털단지역");
break;
case 2:
Log.d("dodo4989","2");
editor.putInt("where", 2);
LM.addProximityAlert(37.4994995976215,
126.9287234544754, 500, -1, mPending);
currentDestination.setText("신대방삼거리역");
break;
case 3:
Log.d("dodo4989","3");
editor.putInt("where", 3);
LM.addProximityAlert(37.483957534386015,
126.93013697862625, 500, -1, mPending);
currentDestination.setText("신림역");
break;
case 4:
Log.d("dodo4989","4");
editor.putInt("where", 4);
LM.addProximityAlert(37.487049,
126.878930, 500, -1, mPending);
currentDestination.setText("회사");
break;
}
editor.commit();
return super.onOptionsItemSelected(item);
}
@Override
protected void onResume() {
super.onResume();
LM.requestLocationUpdates(mBestProvider, 1000, 1, mListener); // 1초마다 위치정보를 받아옴, 1미터 변경시 위치정보를 받아옴.
}
@Override
protected void onPause() {
super.onPause();
LM.removeUpdates(mListener);
LM.removeProximityAlert(mPending);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}