Blame view

app/src/main/java/com/example/filewrite/MainActivity.java 7.12 KB
ae3c0175   José Rolo   Correcção do comm...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package com.example.filewrite;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.media.ToneGenerator;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;


public class MainActivity extends Activity implements SensorEventListener{
	int count;
	//Button btn;
	ToggleButton tb;
	TextView tvX,tvY,tvZ,tvCount;
	private static final String TAG = "MEDIA";
	private SensorManager senSensorManager;
	private Sensor senAccelerometer;
	private StringBuilder data;
	String outputFilename;
	ToneGenerator toneG;
	ProgressBar Zbar;
	ProgressBar Ybar;
	ProgressBar Xbar;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        outputFilename = "";
        data = new StringBuilder(300000);
        count = 0;
        //btn = (Button) findViewById(R.id.button1);
        tb = (ToggleButton)findViewById(R.id.switchBtn);
        tvX = (TextView) findViewById(R.id.tvX);
        tvY = (TextView) findViewById(R.id.tvY);
        tvZ = (TextView) findViewById(R.id.tvZ);
        tvCount = (TextView) findViewById(R.id.tvCount);
        senSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        senAccelerometer = senSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        senSensorManager.registerListener(this, senAccelerometer , SensorManager.SENSOR_DELAY_FASTEST);
        toneG = new ToneGenerator(AudioManager.STREAM_ALARM, 100);
        Xbar = (ProgressBar) findViewById(R.id.progressBar1);
        Ybar = (ProgressBar) findViewById(R.id.progressBar2);
        Zbar = (ProgressBar) findViewById(R.id.progressBar3);
        Xbar.setMax(11);
        Ybar.setMax(11);
        Zbar.setMax(11);
//        btn.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				save();
//			}
//		});
        tb.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				
				if(outputFilename == ""){
					Toast.makeText(getBaseContext(),"Please set the output file first",Toast.LENGTH_LONG).show();
					tb.setChecked(false);
				}else{
					if(tb.isChecked()){
						try {
							Thread.sleep(10000, 0);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					toneG.startTone(ToneGenerator.TONE_DTMF_0, 600); 
				}
				
			}
		});
    }
    
    private void save(){
    	int status = writeToSDFile(data.toString());
		if(status == 0){
			data = new StringBuilder(300000);
		}else{
			 Toast.makeText(getBaseContext(),"Error while saving...",Toast.LENGTH_SHORT).show();
		}
    }
    
    private int writeToSDFile(String s){

        // Find the root of the external storage.
        // See http://developer.android.com/guide/topics/data/data-  storage.html#filesExternal

        File root = android.os.Environment.getExternalStorageDirectory(); 

        //Toast.makeText(getBaseContext(),"\nExternal file system root: "+root,Toast.LENGTH_SHORT).show();
        // See http://stackoverflow.com/questions/3551821/android-write-to-sd-card-folder

        File dir = new File (root.getAbsolutePath() + "/download");
        dir.mkdirs();
        File file = new File(dir, outputFilename);

        try {
        	FileWriter fw = new FileWriter(file,true);
        	fw.write(s);
        	fw.close();
//            FileOutputStream f = new FileOutputStream(file);
//            
//            PrintWriter pw = new PrintWriter(f);
////            pw.println("Hi , How are you");
////            pw.println("Hello");
//            pw.println(s);
//            pw.flush();
//            pw.close();
//            f.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Log.i(TAG, "******* File not found. Did you" +
                    " add a WRITE_EXTERNAL_STORAGE permission to the   manifest?");
            return 1;
        } catch (IOException e) {
            e.printStackTrace();
            return 2;
        }   

        Toast.makeText(getBaseContext(),"File written to "+file,Toast.LENGTH_SHORT).show();
        return 0;
    }
    
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
        	AlertDialog.Builder alert = new AlertDialog.Builder(this);

        	alert.setTitle("Output");
        	alert.setMessage("Please insert output filename.");

        	// Set an EditText view to get user input 
        	final EditText input = new EditText(this);
        	alert.setView(input);

        	alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        	public void onClick(DialogInterface dialog, int whichButton) {
        	  // Do something with value!
        	  Toast.makeText(getBaseContext(),"Filename Set",Toast.LENGTH_SHORT).show();
        	  outputFilename = input.getText().toString();
        	  }
        	});

        	alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        	  public void onClick(DialogInterface dialog, int whichButton) {
        	    // Canceled.
        	  }
        	});

        	alert.show();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

	@Override
	public void onAccuracyChanged(Sensor arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onSensorChanged(SensorEvent arg0) {
		// TODO Auto-generated method stub
		tvX.setText("X: "+String.format("%.3f%n",arg0.values[0]));
		tvY.setText("Y: "+String.format("%.3f%n",arg0.values[1]));
		tvZ.setText("Z: "+String.format("%.3f%n",arg0.values[2]));
		Xbar.setProgress((int)arg0.values[0]);
		Ybar.setProgress((int)arg0.values[1]);
		Zbar.setProgress((int)arg0.values[2]);
		if(tb.isChecked()){
			count++;
			tvCount.setText("Count: "+String.valueOf(count));
			data.append(count+","+arg0.values[0]+","+arg0.values[1]+","+arg0.values[2]+"\n");
			if(count%1000 == 0){
				save();
				toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 150); 
			}
		}
		
	}
}