first commit
This commit is contained in:
		
							
								
								
									
										75
									
								
								app/src/main/java/com/localtransfer/TransferService.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										75
									
								
								app/src/main/java/com/localtransfer/TransferService.java
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,75 @@
 | 
			
		||||
package com.localtransfer;
 | 
			
		||||
 | 
			
		||||
import android.app.IntentService;
 | 
			
		||||
import android.content.Intent;
 | 
			
		||||
import android.net.Uri;
 | 
			
		||||
import android.widget.Button;
 | 
			
		||||
import android.widget.LinearLayout;
 | 
			
		||||
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
 | 
			
		||||
public class TransferService extends IntentService {
 | 
			
		||||
 | 
			
		||||
    public static final String ACTION_UPLOAD = "com.localtransfer.action.FOO";
 | 
			
		||||
    public static final String ACTION_DOWNLOAD = "com.localtransfer.action.BAZ";
 | 
			
		||||
 | 
			
		||||
    public static final String EXTRA_URI = "com.localtransfer.extra.URI";
 | 
			
		||||
    public static final String EXTRA = "com.localtransfer.extra.EXTRA";
 | 
			
		||||
 | 
			
		||||
    public TransferService() {
 | 
			
		||||
        super("TransferService");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected void onHandleIntent(Intent intent) {
 | 
			
		||||
        if (intent != null) {
 | 
			
		||||
            final String action = intent.getAction();
 | 
			
		||||
            switch (action) {
 | 
			
		||||
                case ACTION_UPLOAD:
 | 
			
		||||
                    handleUpload(intent);
 | 
			
		||||
                    break;
 | 
			
		||||
                case ACTION_DOWNLOAD:
 | 
			
		||||
                    handleDownload(intent);
 | 
			
		||||
                    break;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void handleUpload(Intent intent) {
 | 
			
		||||
        final Uri uri = intent.getParcelableExtra(EXTRA_URI);
 | 
			
		||||
 | 
			
		||||
        Transfer tr = new Transfer();
 | 
			
		||||
        try {
 | 
			
		||||
            tr.uploadFile(uri);
 | 
			
		||||
        } catch (IOException e) {
 | 
			
		||||
            final String ExceptionName = e.getClass().getSimpleName();
 | 
			
		||||
            final String ExceptionMess = e.getMessage();
 | 
			
		||||
 | 
			
		||||
            if(ExceptionName != null && ExceptionMess != null) {
 | 
			
		||||
                Transfer.error(ExceptionName + ": " + ExceptionMess, null, null);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void handleDownload(Intent intent) {
 | 
			
		||||
        Extra ex = (Extra) intent.getExtras().getParcelable(EXTRA);
 | 
			
		||||
        final LinearLayout layout = Transfer.activity.findViewById(ex.id);
 | 
			
		||||
        Button button = null;
 | 
			
		||||
        if(layout != null)
 | 
			
		||||
            button = layout.findViewById(R.id.file_download);
 | 
			
		||||
 | 
			
		||||
        Transfer tr = new Transfer();
 | 
			
		||||
        try {
 | 
			
		||||
            tr.downloadFile(Uri.parse(ex.uri), ex.name, ex.size, ex.href, button);
 | 
			
		||||
        } catch (IOException e) {
 | 
			
		||||
            final String ExceptionName = e.getClass().getSimpleName();
 | 
			
		||||
            final String ExceptionMess = e.getMessage();
 | 
			
		||||
 | 
			
		||||
            if(ExceptionName != null && ExceptionMess != null) {
 | 
			
		||||
                Transfer.error(ExceptionName + ": " + ExceptionMess, null, null);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user