<commit_before>package com.tacticalnuclearstrike.tttumblr;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

/** TumblrService - a Service subclass to interact with tumblr's api
 *
 * contains all the relevant bits for making calls to the backend web service.
 *
 * Intents received by this service (prefixed with package name):
 * * POST_TEXT - String title, String body, boolean isPrivate
 * * POST_PHOTO - Uri photo, String caption
 */
public class TumblrService extends Service {
    private static final String TAG = "TumblrService";
    // notification integers.
    public static final int N_POSTING = 1; // we're currently posting something

    //Actions:
    public static final String ACTION_POST_TEXT = "com.tacticalnuclearstrike.tttumblr.POST_TEXT";
    public static final String ACTION_POST_PHOTO = "com.tacticalnuclearstrike.tttumblr.POST_PHOTO";
    public static final String ACTION_POST_CONVERSATION = "com.tacticalnuclearstrike.tttumblr.POST_CONVERSATION";
    public static final String ACTION_POST_QUOTE = "com.tacticalnuclearstrike.tttumblr.POST_QUOTE";
    public static final String ACTION_POST_LINK = "com.tacticalnuclearstrike.tttumblr.POST_LINK";

    @Override
    public void onCreate() {
        Log.d(TAG, "oncreate!");
    }

    @Override
    public IBinder onBind(Intent i){return null;} // dont use onBind.

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "start intent received: " + intent.getAction());
        if (ACTION_POST_TEXT.equals(intent.getAction())){
            doTextPost(intent);
        } else if (ACTION_POST_PHOTO.equals(intent.getAction())) {
            doPhotoPost(intent);
        } else if (ACTION_POST_CONVERSATION.equals(intent.getAction())) {
            doConversationPost(intent);
        } else if (ACTION_POST_QUOTE.equals(intent.getAction())) {
            doQuotePost(intent);
        } else if (ACTION_POST_LINK.equals(intent.getAction())) {
            doUrlPost(intent);
        }
        else {
            Log.d(TAG, "UNKNOWN ACTION!");
        }
        return START_REDELIVER_INTENT;
    }

    //TODO: should these posts be cached somewhere so we can retry?
    private void doTextPost(Intent i){
        final String titleText = i.getStringExtra("title");
        final String postText = i.getStringExtra("body");
        final Bundle options = i.getBundleExtra("options");
		final TumblrApi api = new TumblrApi(this);
        Log.d(TAG, "attempting text post..");
		new Thread(new Runnable() {
			public void run() {
                startForeground(N_POSTING, getNotification("test"));
                Log.d(TAG, "calling api.");
				api.postText(titleText, postText, options);
                stopForeground(true);
			}
		}).start();
    }

    /** doPhotoPost - posts a photo (given extras).
     * Extras: 'photo' - Uri, 'caption' - String.
     */
    private void doPhotoPost(Intent i){
        final Uri photo = Uri.parse(i.getStringExtra("photo"));
        final String text = i.getStringExtra("caption");
        final Bundle options = i.getBundleExtra("options");
		final TumblrApi api = new TumblrApi(this);
		new Thread(new Runnable() {
			public void run() {
                startForeground(N_POSTING, getNotification("photo"));
				api.postImage(photo, text, options);
                stopForeground(true);
			}
		}).start();
    }

    /** doConversationPost - posts a conversation.
     * Extras: 'title' - String, 'conversation' - String.
     */
    private void doConversationPost(Intent i){
        final String title = i.getStringExtra("title");
        final String convo = i.getStringExtra("conversation");
        final Bundle options = i.getBundleExtra("options");
		final TumblrApi api = new TumblrApi(this);
		new Thread(new Runnable() {
			public void run() {
                startForeground(N_POSTING, getNotification("conversation"));
				api.postConversation(title, convo, options);
                stopForeground(true);
			}
		}).start();
    }

    /** doQuotePost - posts a quote
     * Extras: 'quote' - String, 'source' - String (optional).
     */
    private void doQuotePost(Intent i){
        final String quote = i.getStringExtra("quote");
        final String source = i.getStringExtra("source");
        final Bundle options = i.getBundleExtra("options");
		final TumblrApi api = new TumblrApi(this);
		new Thread(new Runnable() {
			public void run() {
                startForeground(N_POSTING, getNotification("quote"));
				api.postQuote(quote, source, options);
                stopForeground(true);
			}
		}).start();
    }

    /** doUrlPost - posts a link
     * Extras: 'link' - String, 'name' - String, 'description' - String
     */
    private void doUrlPost(Intent i){
        final String link = i.getStringExtra("link");
        final String name = i.getStringExtra("name");
        final String description = i.getStringExtra("description");
        final Bundle options = i.getBundleExtra("options");
		final TumblrApi api = new TumblrApi(this);
		new Thread(new Runnable() {
			public void run() {
                startForeground(N_POSTING, getNotification("url"));
				api.postUrl(link, name, description, options);
                stopForeground(true);
			}
		}).start();
    }

    private Notification getNotification(String type){
        Notification n = new Notification(android.R.drawable.stat_notify_sync, "Uploading to Tumblr...", 0);
        Intent i = new Intent("android.intent.action.MAIN");
        i.setClassName("com.tacticalnuclearstrike.tttumblr", "MainActivity");
        PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);
        n.setLatestEventInfo(this, "posting", "posting "+type, pi);
        return n;
    }

    @Override
    public void onDestroy() {
        // Tell the user we stopped.
        Toast.makeText(this, "tumblr service stopped!", Toast.LENGTH_SHORT).show();
    }<commit_msg>Service compatible with 1.6 android (and hopfully 1.5)<commit_after>package com.tacticalnuclearstrike.tttumblr;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

/** TumblrService - a Service subclass to interact with tumblr's api
 *
 * contains all the relevant bits for making calls to the backend web service.
 *
 * Intents received by this service (prefixed with package name):
 * * POST_TEXT - String title, String body, boolean isPrivate
 * * POST_PHOTO - Uri photo, String caption
 */
public class TumblrService extends Service {
    private static final String TAG = "TumblrService";
    // notification integers.
    public static final int N_POSTING = 1; // we're currently posting something

    //Actions:
    public static final String ACTION_POST_TEXT = "com.tacticalnuclearstrike.tttumblr.POST_TEXT";
    public static final String ACTION_POST_PHOTO = "com.tacticalnuclearstrike.tttumblr.POST_PHOTO";
    public static final String ACTION_POST_CONVERSATION = "com.tacticalnuclearstrike.tttumblr.POST_CONVERSATION";
    public static final String ACTION_POST_QUOTE = "com.tacticalnuclearstrike.tttumblr.POST_QUOTE";
    public static final String ACTION_POST_LINK = "com.tacticalnuclearstrike.tttumblr.POST_LINK";

    @Override
    public void onCreate() {
        Log.d(TAG, "oncreate!");
        mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        try {
            mStartForeground = getClass().getMethod("startForeground",
                    mStartForegroundSignature);
            mStopForeground = getClass().getMethod("stopForeground",
                    mStopForegroundSignature);
        } catch (NoSuchMethodException e) {
            // Running on an older platform.
            mStartForeground = mStopForeground = null;
        }
    }
    
    private static final Class[] mStartForegroundSignature = new Class[] {
        int.class, Notification.class};
    private static final Class[] mStopForegroundSignature = new Class[] {
        boolean.class};

    private NotificationManager mNM;
    private Method mStartForeground;
    private Method mStopForeground;
    private Object[] mStartForegroundArgs = new Object[2];
    private Object[] mStopForegroundArgs = new Object[1];

    /**
     * This is a wrapper around the new startForeground method, using the older
     * APIs if it is not available.
     */
    void startForegroundCompat(int id, Notification notification) {
        // If we have the new startForeground API, then use it.
        if (mStartForeground != null) {
            mStartForegroundArgs[0] = Integer.valueOf(id);
            mStartForegroundArgs[1] = notification;
            try {
                mStartForeground.invoke(this, mStartForegroundArgs);
            } catch (InvocationTargetException e) {
                // Should not happen.
                Log.w("ApiDemos", "Unable to invoke startForeground", e);
            } catch (IllegalAccessException e) {
                // Should not happen.
                Log.w("ApiDemos", "Unable to invoke startForeground", e);
            }
            return;
        }

        // Fall back on the old API.
        setForeground(true);
        mNM.notify(id, notification);
    }

    /**
     * This is a wrapper around the new stopForeground method, using the older
     * APIs if it is not available.
     */
    void stopForegroundCompat(Boolean value) {
        // If we have the new stopForeground API, then use it.
        if (mStopForeground != null) {
            mStopForegroundArgs[0] = Boolean.TRUE;
            try {
                mStopForeground.invoke(this, mStopForegroundArgs);
            } catch (InvocationTargetException e) {
                // Should not happen.
                Log.w("ApiDemos", "Unable to invoke stopForeground", e);
            } catch (IllegalAccessException e) {
                // Should not happen.
                Log.w("ApiDemos", "Unable to invoke stopForeground", e);
            }
            return;
        }

        // Fall back on the old API.  Note to cancel BEFORE changing the
        // foreground state, since we could be killed at that point.
        //mNM.cancel(id);
        setForeground(false);
    }

    @Override
    public IBinder onBind(Intent i){return null;} // dont use onBind.
    
    @Override
    public void onStart(Intent intent, int startId) {
        handleCommand(intent);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        handleCommand(intent);
        return START_REDELIVER_INTENT;
    }

	private void handleCommand(Intent intent) {
		Log.d(TAG, "start intent received: " + intent.getAction());
        if (ACTION_POST_TEXT.equals(intent.getAction())){
            doTextPost(intent);
        } else if (ACTION_POST_PHOTO.equals(intent.getAction())) {
            doPhotoPost(intent);
        } else if (ACTION_POST_CONVERSATION.equals(intent.getAction())) {
            doConversationPost(intent);
        } else if (ACTION_POST_QUOTE.equals(intent.getAction())) {
            doQuotePost(intent);
        } else if (ACTION_POST_LINK.equals(intent.getAction())) {
            doUrlPost(intent);
        }
        else {
            Log.d(TAG, "UNKNOWN ACTION!");
        }
	}

    //TODO: should these posts be cached somewhere so we can retry?
    private void doTextPost(Intent i){
        final String titleText = i.getStringExtra("title");
        final String postText = i.getStringExtra("body");
        final Bundle options = i.getBundleExtra("options");
		final TumblrApi api = new TumblrApi(this);
        Log.d(TAG, "attempting text post..");
		new Thread(new Runnable() {
			public void run() {
				startForegroundCompat(N_POSTING, getNotification("text"));
                Log.d(TAG, "calling api.");
				api.postText(titleText, postText, options);
				stopForegroundCompat(true);
			}
		}).start();
    }

    /** doPhotoPost - posts a photo (given extras).
     * Extras: 'photo' - Uri, 'caption' - String.
     */
    private void doPhotoPost(Intent i){
        final Uri photo = Uri.parse(i.getStringExtra("photo"));
        final String text = i.getStringExtra("caption");
        final Bundle options = i.getBundleExtra("options");
		final TumblrApi api = new TumblrApi(this);
		new Thread(new Runnable() {
			public void run() {
				startForegroundCompat(N_POSTING, getNotification("photo"));
				api.postImage(photo, text, options);
				stopForegroundCompat(true);
			}
		}).start();
    }

    /** doConversationPost - posts a conversation.
     * Extras: 'title' - String, 'conversation' - String.
     */
    private void doConversationPost(Intent i){
        final String title = i.getStringExtra("title");
        final String convo = i.getStringExtra("conversation");
        final Bundle options = i.getBundleExtra("options");
		final TumblrApi api = new TumblrApi(this);
		new Thread(new Runnable() {
			public void run() {
				startForegroundCompat(N_POSTING, getNotification("conversation"));
				api.postConversation(title, convo, options);
				stopForegroundCompat(true);
			}
		}).start();
    }

    /** doQuotePost - posts a quote
     * Extras: 'quote' - String, 'source' - String (optional).
     */
    private void doQuotePost(Intent i){
        final String quote = i.getStringExtra("quote");
        final String source = i.getStringExtra("source");
        final Bundle options = i.getBundleExtra("options");
		final TumblrApi api = new TumblrApi(this);
		new Thread(new Runnable() {
			public void run() {
				startForegroundCompat(N_POSTING, getNotification("quote"));
				api.postQuote(quote, source, options);
				stopForegroundCompat(true);
			}
		}).start();
    }

    /** doUrlPost - posts a link
     * Extras: 'link' - String, 'name' - String, 'description' - String
     */
    private void doUrlPost(Intent i){
        final String link = i.getStringExtra("link");
        final String name = i.getStringExtra("name");
        final String description = i.getStringExtra("description");
        final Bundle options = i.getBundleExtra("options");
		final TumblrApi api = new TumblrApi(this);
		new Thread(new Runnable() {
			public void run() {
				startForegroundCompat(N_POSTING, getNotification("url"));
				api.postUrl(link, name, description, options);
				stopForegroundCompat(true);
			}
		}).start();
    }

    private Notification getNotification(String type){
        Notification n = new Notification(android.R.drawable.stat_notify_sync, "Uploading to Tumblr...", 0);
        Intent i = new Intent("android.intent.action.MAIN");
        i.setClassName("com.tacticalnuclearstrike.tttumblr", "MainActivity");
        PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);
        n.setLatestEventInfo(this, "posting", "posting "+type, pi);
        return n;
    }

    @Override
    public void onDestroy() {
        // Tell the user we stopped.
        Toast.makeText(this, "tumblr service stopped!", Toast.LENGTH_SHORT).show();
        stopForegroundCompat(true);
    }
{'fraction_non_alphanumeric': 0.10984824023248305, 'fraction_numerical': 0.0010978366160800774, 'mean_word_length': 2.8657014478282576, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5448635', 'n_tokens_mistral': 4685, 'n_tokens_neox': 4274, 'n_words': 1148}