<commit_before>package com.danga.camli;

import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;

import android.app.Service;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.os.IBinder;
import android.os.ParcelFileDescriptor;
import android.os.PowerManager;
import android.os.RemoteException;
import android.util.Log;

public class UploadService extends Service {
    private static final String TAG = "UploadService";

    // Everything in this block guarded by 'this':
    private boolean mUploading = false; // user's desired state (notified
                                        // quickly)
    private UploadThread mUploadThread = null; // last thread created; null when
                                               // thread exits
    private final Set<QueuedFile> mQueueSet = new HashSet<QueuedFile>();
    private final LinkedList<QueuedFile> mQueueList = new LinkedList<QueuedFile>();
    private IStatusCallback mCallback = DummyNullCallback.instance();
    private String mLastUploadStatusText = null;

    // Stats, all guarded by 'this', and all reset to 0 on queue size transition from 0 -> 1.
    private long mBytesTotal = 0;
    private long mBytesUploaded = 0;
    private int mBlobsTotal = 0;
    private int mBlobsUploaded = 0;

    // Effectively final, initialized in onCreate():
    PowerManager mPowerManager;
    WifiManager mWifiManager;

    @Override
    public void onCreate() {
        super.onCreate();
        mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
        mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "UPLOAD SERVICE onDestroy !!!");
    }

	@Override
    public IBinder onBind(Intent intent) {
        return service;
	}

    // Called by UploadThread to get stuff to do. Caller owns the returned new
    // LinkedList. Doesn't return null.
    LinkedList<QueuedFile> uploadQueue() {
        synchronized (this) {
            LinkedList<QueuedFile> copy = new LinkedList<QueuedFile>();
            copy.addAll(mQueueList);
            return copy;
        }
    }

    void setUploadStatusText(String status) {
        IStatusCallback cb;
        synchronized (this) {
            mLastUploadStatusText = status;
            cb = mCallback;
        }
        try {
            cb.setUploadStatusText(status);
        } catch (RemoteException e) {
        }
    }

    private void onUploadThreadEnded() {
        synchronized (this) {
            Log.d(TAG, "UploadThread ended.");
            mUploadThread = null;
            mUploading = false;
            try {
                mCallback.setUploading(false);
            } catch (RemoteException e) {
            }
        }
    }

    void onUploadComplete(QueuedFile qf, boolean wasAlreadyExisting) {
        synchronized (this) {
            if (!mQueueSet.remove(qf)) {
                return;
            }
            mQueueList.remove(qf); // TODO: ghetto, linear scan

            mBytesUploaded += qf.getSize();
            mBlobsUploaded += 1;
            try {
                mCallback.setBlobsRemain(mQueueSet.size());
                mCallback.setBlobStatus(mBlobsUploaded, mBlobsTotal);
                mCallback.setByteStatus(mBytesUploaded, mBytesTotal);
            } catch (RemoteException e) {
            }

            if (mQueueSet.isEmpty()) {
                stopService(new Intent(UploadService.this, UploadService.class));
            }
        }
    }

    ParcelFileDescriptor getFileDescriptor(Uri uri) {
        ContentResolver cr = getContentResolver();
        try {
            return cr.openFileDescriptor(uri, "r");
        } catch (FileNotFoundException e) {
            Log.w(TAG, "FileNotFound for " + uri, e);
            return null;
        }
    }

    private final IUploadService.Stub service = new IUploadService.Stub() {

        public boolean enqueueUpload(Uri uri) throws RemoteException {
            startService(new Intent(UploadService.this, UploadService.class));
            SharedPreferences sp = getSharedPreferences(Preferences.NAME, 0);
            HostPort hp = new HostPort(sp.getString(Preferences.HOST, ""));
            if (!hp.isValid()) {
                return false;
            }

            ParcelFileDescriptor pfd = getFileDescriptor(uri);

            String sha1 = Util.getSha1(pfd.getFileDescriptor());
            QueuedFile qf = new QueuedFile(sha1, uri, pfd.getStatSize());

            boolean needResume = false;
            synchronized (UploadService.this) {
                if (mQueueSet.contains(qf)) {
                    Log.d(TAG, "Dup blob enqueue, ignoring " + qf);
                    return false;
                }
                Log.d(TAG, "Enqueueing blob: " + qf);
                mQueueSet.add(qf);
                mQueueList.add(qf);
<commit_msg>stop service in more cases<commit_after>package com.danga.camli;

import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;

import android.app.Service;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.os.IBinder;
import android.os.ParcelFileDescriptor;
import android.os.PowerManager;
import android.os.RemoteException;
import android.util.Log;

public class UploadService extends Service {
    private static final String TAG = "UploadService";

    // Everything in this block guarded by 'this':
    private boolean mUploading = false; // user's desired state (notified
                                        // quickly)
    private UploadThread mUploadThread = null; // last thread created; null when
                                               // thread exits
    private final Set<QueuedFile> mQueueSet = new HashSet<QueuedFile>();
    private final LinkedList<QueuedFile> mQueueList = new LinkedList<QueuedFile>();
    private IStatusCallback mCallback = DummyNullCallback.instance();
    private String mLastUploadStatusText = null;

    // Stats, all guarded by 'this', and all reset to 0 on queue size transition from 0 -> 1.
    private long mBytesTotal = 0;
    private long mBytesUploaded = 0;
    private int mBlobsTotal = 0;
    private int mBlobsUploaded = 0;

    // Effectively final, initialized in onCreate():
    PowerManager mPowerManager;
    WifiManager mWifiManager;

    @Override
    public void onCreate() {
        super.onCreate();
        mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
        mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "UPLOAD SERVICE onDestroy !!!");
    }

	@Override
    public IBinder onBind(Intent intent) {
        return service;
	}

    // Called by UploadThread to get stuff to do. Caller owns the returned new
    // LinkedList. Doesn't return null.
    LinkedList<QueuedFile> uploadQueue() {
        synchronized (this) {
            LinkedList<QueuedFile> copy = new LinkedList<QueuedFile>();
            copy.addAll(mQueueList);
            return copy;
        }
    }

    void setUploadStatusText(String status) {
        IStatusCallback cb;
        synchronized (this) {
            mLastUploadStatusText = status;
            cb = mCallback;
        }
        try {
            cb.setUploadStatusText(status);
        } catch (RemoteException e) {
        }
    }

    private void onUploadThreadEnded() {
        synchronized (this) {
            Log.d(TAG, "UploadThread ended.");
            mUploadThread = null;
            mUploading = false;
            try {
                mCallback.setUploading(false);
            } catch (RemoteException e) {
            }
        }
    }

    void onUploadComplete(QueuedFile qf, boolean wasAlreadyExisting) {
        synchronized (this) {
            if (!mQueueSet.remove(qf)) {
                return;
            }
            mQueueList.remove(qf); // TODO: ghetto, linear scan

            mBytesUploaded += qf.getSize();
            mBlobsUploaded += 1;
            try {
                mCallback.setBlobsRemain(mQueueSet.size());
                mCallback.setBlobStatus(mBlobsUploaded, mBlobsTotal);
                mCallback.setByteStatus(mBytesUploaded, mBytesTotal);
            } catch (RemoteException e) {
            }
        }
        stopServiceIfEmpty();
    }

    private void stopServiceIfEmpty() {
        synchronized (this) {
            if (mQueueSet.isEmpty()) {
                stopService(new Intent(UploadService.this, UploadService.class));
            }
        }
    }

    ParcelFileDescriptor getFileDescriptor(Uri uri) {
        ContentResolver cr = getContentResolver();
        try {
            return cr.openFileDescriptor(uri, "r");
        } catch (FileNotFoundException e) {
            Log.w(TAG, "FileNotFound for " + uri, e);
            return null;
        }
    }

    private final IUploadService.Stub service = new IUploadService.Stub() {

        public boolean enqueueUpload(Uri uri) throws RemoteException {
            startService(new Intent(UploadService.this, UploadService.class));
            SharedPreferences sp = getSharedPreferences(Preferences.NAME, 0);
            HostPort hp = new HostPort(sp.getString(Preferences.HOST, ""));
            if (!hp.isValid()) {
                stopServiceIfEmpty();
                return false;
            }

            ParcelFileDescriptor pfd = getFileDescriptor(uri);

            String sha1 = Util.getSha1(pfd.getFileDescriptor());
            QueuedFile qf = new QueuedFile(sha1, uri, pfd.getStatSize());

            boolean needResume = false;
            synchronized (UploadService.this) {
                if (mQueueSet.contains(qf)) {
                    Log.d(TAG, "Dup blob enqueue, ignoring " + qf);
                    stopServiceIfEmpty();
                    return false;
                }
                Log.d(TAG, "Enqueueing blob: " + qf);
                mQueueSet.add(qf);
                mQueueList.add(qf);

{'fraction_non_alphanumeric': 0.09132638424022187, 'fraction_numerical': 0.0022951133212202354, 'mean_word_length': 2.137713771377138, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6815389', 'n_tokens_mistral': 2855, 'n_tokens_neox': 2778, 'n_words': 682}