You are on page 1of 6

/* * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.

0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.google.SimpleButton; import import import import import import import import import import import import import import import import import import import import import import import import import java.io.FileDescriptor; java.io.FileInputStream; java.io.FileOutputStream; java.io.IOException; java.util.ArrayList; org.apache.http.HttpResponse; org.apache.http.NameValuePair; org.apache.http.client.HttpClient; org.apache.http.client.entity.UrlEncodedFormEntity; org.apache.http.client.methods.HttpPost; org.apache.http.impl.client.DefaultHttpClient; org.apache.http.message.BasicNameValuePair; android.app.Activity; android.app.PendingIntent; android.content.BroadcastReceiver; android.content.Context; android.content.Intent; android.content.IntentFilter; android.location.Location; android.location.LocationListener; android.location.LocationManager; android.os.Bundle; android.os.ParcelFileDescriptor; android.util.Log; android.widget.TextView;

import com.android.future.usb.UsbAccessory; import com.android.future.usb.UsbManager; public class SimpleButtonActivity extends Activity { private static final String TAG = "SimpleButtonActivity"; private PendingIntent mPermissionIntent; private static final String ACTION_USB_PERMISSION = "com.google.android. SimpleButton.action.USB_PERMISSION"; private boolean mPermissionRequestPending; private UsbManager mUsbManager;

private private private private private private private private

UsbAccessory mAccessory; ParcelFileDescriptor mFileDescriptor; FileInputStream mInputStream; FileOutputStream mOutputStream; static static static static final final final final byte byte byte byte COMMAND_BUTTON = 0x1; TARGET_BUTTON = 0x1; VALUE_ON = 0x1; VALUE_OFF = 0x0;

private static final String BUTTON_PRESSED_TEXT = "Peringatan! Ada kecel akaan."; private static final String BUTTON_NOT_PRESSED_TEXT = "Hati-hati dalam b erkendara!"; private LocationManager lm; private LocationListener locListener; private TextView buttonStateTextView; private TextView latTxt, lonTxt; public double lon; public double lat; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mUsbManager = UsbManager.getInstance(this); mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTIO N_USB_PERMISSION), 0); IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION); filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED); registerReceiver(mUsbReceiver, filter); lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); locListener = new MyLocationListener(); lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 90000, 900, locL istener); setContentView(R.layout.main); buttonStateTextView = (TextView) findViewById(R.id.button_state_text_vie w); latTxt = (TextView) findViewById(R.id.latitudeTxt); lonTxt = (TextView) findViewById(R.id.longitudeTxt); } @Override public void onResume(){ super.onResume(); if (mInputStream != null && mOutputStream != null){ return; } UsbAccessory[] accessories = mUsbManager.getAccessoryList(); UsbAccessory accessory = (accessories == null ? null : accessories[0]);

if (accessory != null){ if (mUsbManager.hasPermission(accessory)){ openAccessory(accessory); }else { synchronized (mUsbReceiver){ if (!mPermissionRequestPending){ mUsbManager.requestPermission(accessory, mPermissionIntent); mPermissionRequestPending = true; } } } }else { Log.d(TAG, "mAccessory is null"); } } @Override public void onPause(){ super.onPause(); closeAccessory(); } @Override public void onDestroy(){ super.onDestroy(); unregisterReceiver(mUsbReceiver); } private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent){ String action = intent.getAction(); if (ACTION_USB_PERMISSION.equals(action)){ synchronized (this){ UsbAccessory accessory = UsbManager.getAccessory (intent); if (intent.getBooleanExtra(UsbManager.EXTRA_PERM ISSION_GRANTED,false)){ openAccessory(accessory); }else { Log.d(TAG, "permission denied for access ory" + accessory); } mPermissionRequestPending = false; } } else if (UsbManager.ACTION_USB_ACCESSORY_DETACHED.equals(actio n)){ UsbAccessory accessory = UsbManager.getAccessory(intent) ; if (accessory != null && accessory.equals(mAccessory)){ closeAccessory(); } } } }; private void openAccessory(UsbAccessory accessory){

mFileDescriptor = mUsbManager.openAccessory(accessory); if (mFileDescriptor != null) { mAccessory = accessory; FileDescriptor fd = mFileDescriptor.getFileDescriptor(); mInputStream = new FileInputStream(fd); mOutputStream = new FileOutputStream (fd); Thread thread = new Thread(null, commRunnable, TAG); thread.start(); Log.d(TAG, "accessory opened"); }else{ Log.d(TAG, "accessory open fail"); } } private void closeAccessory(){ try{ if (mFileDescriptor != null){ mFileDescriptor.close(); } }catch (IOException e){ }finally { mFileDescriptor = null; mAccessory = null; } } Runnable commRunnable = new Runnable(){ public void run(){ int ret = 0; final byte[] buffer = new byte[3]; while (ret >= 0){ try{ ret = mInputStream.read(buffer); } catch (IOException e) { break; } switch (buffer[0]){ case COMMAND_BUTTON: if(buffer[1] == TARGET_BUTTON){ if(buffer[2] == VALUE_ON){ }else if(buffer[2] == VALUE_OFF){ } runOnUiThread(new Runnable(){ public void run(){ if(buffer[2] == VALUE_ON ){ buttonStateTextV iew.setText(BUTTON_PRESSED_TEXT); //inimi latTxt.setText(S tring.valueOf(lat)); lonTxt.setText(S tring.valueOf(lon));

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); nameValuePairs.add(n ew BasicNameValuePair("y",String.valueOf(lon))); nameValuePairs.add(n ew BasicNameValuePair("x",String.valueOf(lat))); try{ HttpClient h ttpclient = new DefaultHttpClient(); HttpPost htt ppost = new Http Post("http://192.168.77.92/coba2/proses.php"); httppost.set Entity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); Log.i("postD ata", response.getStatusLine().toString()); } catch(Exception e) { Log.e("log_t ag", "Error in http connection "+e.toString()); } }else{ buttonStateTextV iew.setText(BUTTON_NOT_PRESSED_TEXT); } } }); } break; default: Log.d(TAG, "unknown msg: " + buffer[0]); break; } } } }; public class MyLocationListener implements LocationListener{ public void onLocationChanged(Location loc){ if (loc != null){ lat = loc.getLatitude();

lon = loc.getLongitude(); } } public void onProviderDisabled(String provider) { // TODO Auto-generated method stub } public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } }

You might also like