Skip to content

Commit

Permalink
Cleanup service code and make it sticky and start on create
Browse files Browse the repository at this point in the history
  • Loading branch information
majido committed Dec 7, 2014
1 parent 1a75117 commit ff84950
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 60 deletions.
36 changes: 18 additions & 18 deletions src/main/java/ca/zgrs/clipper/ClipboardService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,34 @@

import android.util.Log;

import ca.zgrs.clipper.Clipper;

public class ClipboardService extends IntentService {
private static String TAG = "Clipboard service";
private static String TAG = "ClipboardService";


public ClipboardService() {
super("ClipboardService");
}

/**
/* Define service as sticky so that it stays in background */
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}

@Override
public void onCreate() {
super.onCreate();
//start itself to ensure our broadcast receiver is active
Log.d(TAG, "Start clipboard service.");
startService(new Intent(getApplicationContext(),ClipboardService.class));
}

/**
* The IntentService calls this method from the default worker thread with
* the intent that started the service. When this method returns, IntentService
* stops the service, as appropriate.
*/
@Override
protected void onHandleIntent(Intent intent) {
Log.d(TAG, "Inside intent handler!");
Log.d(TAG, String.format("Action: %s, Text: %s", intent.getAction(), intent.getStringExtra(Clipper.EXTRA_TEXT)));

ClipboardManager cb = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
if (Clipper.isActionSet(intent.getAction())) {
Log.d(TAG, "Setting text into clipboard");
cb.setText( intent.getStringExtra(Clipper.EXTRA_TEXT));
} else if (Clipper.isActionGet( intent.getAction()) ) {
Log.d(TAG, "Getting text from clipboard");
String clip = cb.getText().toString();
Log.i(TAG, "Clipboard text:"+clip);
}
}
protected void onHandleIntent(Intent intent) {}
}
20 changes: 0 additions & 20 deletions src/main/java/ca/zgrs/clipper/Clipper.java

This file was deleted.

73 changes: 51 additions & 22 deletions src/main/java/ca/zgrs/clipper/ClipperReceiver.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,67 @@
package ca.zgrs.clipper;

import android.app.Activity;
import android.text.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.BroadcastReceiver;

import android.util.Log;

import ca.zgrs.clipper.Clipper;
/* Same implementatio just listen to broadcasts */
/*
* Receives broadcast commands and controls clipboard accordingly.
* The broadcast receiver is active only as long as the application, or its service is active.
*/
public class ClipperReceiver extends BroadcastReceiver {
private static String TAG = "Clipboard receiver";

private static String TAG = "ClipboardReceiver";

public static String ACTION_GET = "clipper.get";
public static String ACTION_GET_SHORT = "get";
public static String ACTION_SET = "clipper.set";
public static String ACTION_SET_SHORT = "set";

public static String EXTRA_TEXT = "text";

public static boolean isActionGet(final String action) {
return ACTION_GET.equals(action) || ACTION_GET_SHORT.equals(action);
}

public static boolean isActionSet(final String action) {
return ACTION_SET.equals(action) || ACTION_SET_SHORT.equals(action);
}


@Override
public void onReceive(Context context, Intent intent) {

Log.d(TAG, "Inside broadcast handler!");
Log.d(TAG, String.format("Action: %s, Text: %s", intent.getAction(), intent.getStringExtra(Clipper.EXTRA_TEXT)));

ClipboardManager cb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
if (Clipper.isActionSet(intent.getAction())) {
Log.d(TAG, "Setting text into clipboard");
cb.setText(intent.getStringExtra(Clipper.EXTRA_TEXT));
setResultCode(1);
setResultData("Success!");
} else if (Clipper.isActionGet( intent.getAction()) ) {
Log.d(TAG, "Getting text from clipboard");
String clip = cb.getText().toString();
Log.d(TAG, String.format(" dd Clipboard text: %s",clip));
setResultCode(1);
setResultData(clip);
}

Log.d(TAG, "Inside broadcast handler!");

Log.d(TAG, String.format("Action: %s", intent.getAction()));

ClipboardManager cb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
if (isActionSet(intent.getAction())) {
Log.d(TAG, "Setting text into clipboard");
String text = intent.getStringExtra(EXTRA_TEXT);
if (text != null) {
cb.setText(text);
setResultCode(Activity.RESULT_OK);
setResultData("Text is copied in clipboard.");
} else {
setResultCode(Activity.RESULT_CANCELED);
setResultData("No text was provided. Use -e text \"text to be pasted\"");
}
} else if (isActionGet(intent.getAction())) {
Log.d(TAG, "Getting text from clipboard");
CharSequence clip = cb.getText();
if (clip != null) {
Log.d(TAG, String.format(" dd Clipboard text: %s", clip));
setResultCode(Activity.RESULT_OK);
setResultData(clip.toString());
} else {
setResultCode(Activity.RESULT_CANCELED);
setResultData("Clipboard is empty.");
}
}
}
//android.intent.action.PACKAGE_ADDED

}

0 comments on commit ff84950

Please sign in to comment.