Subversion Repository Public Repository

Nextrek

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
package nextrek.photoupload;

import nextrek.photoupload.R;
import nextrek.services.ClientConnector;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;

import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import android.net.Uri;
import android.content.ContextWrapper;
import android.content.Intent;


import java.io.File;

public class MainActivity extends Activity {

    public static final int ACTIVITY_RESULT_CAMERA = 0xDEAD;

    private static final String TAG = "PhotoUpload.Main";

    private File filesDir;

    private EditText textViewPratica;
    private EditText textViewDescrizione;
    private ImageButton btnSend;

    private TextView mCallbackText;
    
    private synchronized void doLog(String msg)
    {
        Log.e(TAG, msg);
        mCallbackText.setText(msg+"\n"+mCallbackText.getText().toString());
    }
    
    private ClientConnector mClientConnector = new ClientConnector(MainActivity.this,UploadService.class) {

        @Override
        public void onHandleResponse(int result, Bundle bundle) {
            String target = bundle.getString("pratica");
            
            switch (result) {
                case ClientConnector.MSG_WORK_FAILED:
                    doLog(target+": failed");
                    break;
                
                case ClientConnector.MSG_WORK_SUCCEEDED:
                    doLog(target+": succeded");
                    break;
                    
                case ClientConnector.MSG_WORK_PROGRESS:
                    doLog(target+": " + bundle.getString("work-progress"));
                    break;
                    
                default:
                    break;
            }
        }
        
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            // SD-card available
            //filesDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/" + getPackageName());
           filesDir = new File( Environment.getExternalStorageDirectory(), getPackageName()+"/" );
        } else {
            final File internalCacheDir = getCacheDir();
            // apparently on some configurations this can come back as null
            if (internalCacheDir == null) {
                ContextWrapper contextWrapper = new ContextWrapper(this);
                filesDir = contextWrapper.getFilesDir();
            } else {
                filesDir = internalCacheDir;
            }
        }
        
        Log.e(TAG, "camera path: "+filesDir.getPath());

        textViewPratica = (EditText) findViewById(R.id.editTextPratica);
        textViewDescrizione = (EditText) findViewById(R.id.editTextDescrizione);

        ImageButton btnCamera = (ImageButton) findViewById(R.id.imageButtonCamera);
        btnCamera.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                launchCamera();
            }
        });

        btnSend = (ImageButton) findViewById(R.id.imageButtonSend);
        btnSend.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                uploadPicture();
            }
        });

        mCallbackText = (TextView) findViewById(R.id.status);
                
        mClientConnector.BindService();
    }
 

    @Override
    protected void onResume() {
        super.onResume();
        mCallbackText = (TextView) findViewById(R.id.status);
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        mClientConnector.UnbindService();
    }

    private Uri mCameraUri; // ?? WHY ??

    public void launchCamera() {

        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        File path = filesDir;
        if (!path.exists()) {
            boolean pathCreated = path.mkdirs();
            if (!pathCreated) {
                // DataModel.getInstance().showAlertMain(getResources().getString(R.string.camera_error));
                Log.e(TAG, "Error!!!");
                return;
            }
        }
        File photo = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");

        // File photo = new File(path, "camerashot.jpg");
        mCameraUri = Uri.fromFile(photo);

        String filePath = mCameraUri.getPath();

        Log.e(TAG, "photo path: " + filePath);

        // Intent Extra to store the captured image at the mentioned path
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCameraUri);

        MainActivity.this.startActivityForResult(cameraIntent, ACTIVITY_RESULT_CAMERA);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.e(TAG, "onActivityResult " + requestCode + " " + resultCode + " " + (data != null ? data.toString() : ""));

        switch (requestCode) {
            case ACTIVITY_RESULT_CAMERA: {
                onCameraResult(resultCode, data);
                break;
            }

            default:
                break;
        }
    }

    /**
     * Receives the result from Camera
     */
    private void onCameraResult(int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            String filePath = mCameraUri.getPath();
            Log.d(TAG, "onCameraResult: " + filePath);
            // setAttachment(filePath, filePath);
            btnSend.setEnabled(true);
        } else {
            Log.d(TAG, "onCameraResult code " + resultCode);
            btnSend.setEnabled(false);
        }
    }

    private void uploadPicture() {
        String filePath = mCameraUri.getPath();
        Log.d(TAG, "onCameraResult: " + filePath);
/*
        for(Integer s=0; s<10; s++)
        {
            Bundle bundle = new Bundle();
            bundle.putString("photo", filePath);
            bundle.putString("pratica", textViewPratica.getText().toString()+s.toString());
            bundle.putString("descrizione", textViewDescrizione.getText().toString());
            mClientConnector.sendMessage(bundle);
        }
*/
            Bundle bundle = new Bundle();
            bundle.putString("photo", filePath);
            bundle.putString("pratica", textViewPratica.getText().toString());
            bundle.putString("descrizione", textViewDescrizione.getText().toString());
            mClientConnector.sendMessage(bundle);
    }


}

Commits for Nextrek/Android/PhotoUpload/src/nextrek/photoupload/MainActivity.java

Diff revisions: vs.
Revision Author Commited Message
4 FMMortaroli picture FMMortaroli Fri 19 Apr, 2013 16:54:38 +0000