Skip to content

Commit

Permalink
Minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
majido committed Jan 31, 2015
1 parent 3db6094 commit 4a8d970
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 30 deletions.
27 changes: 13 additions & 14 deletions src/main/java/ca/zgrs/clipper/ClipboardService.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package ca.zgrs.clipper;

import android.text.ClipboardManager;
import android.app.IntentService;
import android.content.Intent;

import android.util.Log;

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


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

/* Define service as sticky so that it stays in background */
@Override
Expand All @@ -24,16 +22,17 @@ public int onStartCommand(Intent intent, int flags, int startId) {
@Override
public void onCreate() {
super.onCreate();
//start itself to ensure our broadcast receiver is active
// start itself to ensure our broadcast receiver is active
Log.d(TAG, "Start clipboard service.");
startService(new Intent(getApplicationContext(),ClipboardService.class));
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) {}
* 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) {
}
}
22 changes: 7 additions & 15 deletions src/main/java/ca/zgrs/clipper/ClipperReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
* 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 = "ClipboardReceiver";
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) {
Expand All @@ -30,38 +29,31 @@ 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", 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.");
setResultData("Text is copied into clipboard.");
} else {
setResultCode(Activity.RESULT_CANCELED);
setResultData("No text was provided. Use -e text \"text to be pasted\"");
setResultData("No text is provided. Use -e text \"text to be pasted\"");
}
} else if (isActionGet(intent.getAction())) {
Log.d(TAG, "Getting text from clipboard");
CharSequence clip = cb.getText();
CharSequence clip = cb.getText();
if (clip != null) {
Log.d(TAG, String.format(" dd Clipboard text: %s", clip));
Log.d(TAG, String.format("Clipboard text: %s", clip));
setResultCode(Activity.RESULT_OK);
setResultData(clip.toString());
} else {
setResultCode(Activity.RESULT_CANCELED);
setResultData("Clipboard is empty.");
setResultData("");
}
}
}
}
//android.intent.action.PACKAGE_ADDED

}
2 changes: 1 addition & 1 deletion src/main/java/ca/zgrs/clipper/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void onCreate(Bundle savedInstanceState) {

setContentView(R.layout.main);

//start clipboard service
// start clipboard service
Intent serviceIntent = new Intent(this, ClipboardService.class);
startService(serviceIntent);
}
Expand Down

0 comments on commit 4a8d970

Please sign in to comment.