Revision f721b462

View differences:

src/info/guardianproject/checkey/AppEntry.java
51 51
        return info.packageName;
52 52
    }
53 53

  
54
    public File getApkFile() {
55
        return apkFile;
56
    }
57

  
54 58
    public String getLabel() {
55 59
        return label;
56 60
    }
src/info/guardianproject/checkey/AppListFragment.java
20 20

  
21 21
package info.guardianproject.checkey;
22 22

  
23
import android.annotation.TargetApi;
24 23
import android.content.Intent;
25 24
import android.net.Uri;
26 25
import android.os.Bundle;
......
29 28
import android.support.v4.content.Loader;
30 29
import android.view.View;
31 30
import android.webkit.WebView;
31
import android.widget.AdapterView;
32
import android.widget.AdapterView.OnItemLongClickListener;
32 33
import android.widget.ListView;
33 34

  
34 35
import java.util.List;
35 36

  
36
public class AppListFragment extends ListFragment implements LoaderCallbacks<List<AppEntry>> {
37
public class AppListFragment extends ListFragment
38
        implements LoaderCallbacks<List<AppEntry>>, OnItemLongClickListener {
37 39

  
38 40
    private AppListAdapter adapter;
39 41
    WebView androidObservatoryView;
......
48 50
        setListAdapter(adapter);
49 51
        setListShown(false);
50 52

  
53
        getListView().setOnItemLongClickListener(this);
54

  
51 55
        // Prepare the loader
52 56
        // either reconnect with an existing one or start a new one
53 57
        getLoaderManager().initLoader(0, null, this);
54 58
    }
55 59

  
56
    @TargetApi(11)
57
    // TODO replace with appcompat-v7
58 60
    @Override
59 61
    public void onListItemClick(ListView l, View v, int position, long id) {
60 62
        AppEntry appEntry = (AppEntry) adapter.getItem(position);
61 63
        Intent intent = new Intent(getActivity(), WebViewActivity.class);
62
        String urlString = "https://androidobservatory.org/?searchby=pkg&q=" + appEntry.getPackageName();
64
        String urlString = "https://androidobservatory.org/?searchby=binhash&q="
65
                + Utils.getBinaryHash(appEntry.getApkFile(), "sha1");
66
        intent.setData(Uri.parse(urlString));
67
        intent.putExtra(Intent.EXTRA_TITLE, R.string.by_apk_hash);
68
        startActivity(intent);
69
    }
70

  
71
    @Override
72
    public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
73
        AppEntry appEntry = (AppEntry) adapter.getItem(position);
74
        Intent intent = new Intent(getActivity(), WebViewActivity.class);
75
        String urlString = "https://androidobservatory.org/?searchby=pkg&q="
76
                + appEntry.getPackageName();
63 77
        intent.setData(Uri.parse(urlString));
64 78
        intent.putExtra(Intent.EXTRA_TITLE, R.string.by_package_name);
65 79
        startActivity(intent);
80
        return true;
66 81
    }
67 82

  
68 83
    @Override
src/info/guardianproject/checkey/Utils.java
1

  
2
package info.guardianproject.checkey;
3

  
4
import android.util.Log;
5

  
6
import java.io.BufferedInputStream;
7
import java.io.File;
8
import java.io.FileInputStream;
9
import java.io.IOException;
10
import java.math.BigInteger;
11
import java.security.MessageDigest;
12
import java.security.NoSuchAlgorithmException;
13

  
14
public final class Utils {
15

  
16
    public static String getBinaryHash(File apk, String algo) {
17
        FileInputStream fis = null;
18
        BufferedInputStream bis = null;
19
        try {
20
            MessageDigest md = MessageDigest.getInstance(algo);
21
            fis = new FileInputStream(apk);
22
            bis = new BufferedInputStream(fis);
23

  
24
            byte[] dataBytes = new byte[524288];
25
            int nread = 0;
26

  
27
            while ((nread = bis.read(dataBytes)) != -1)
28
                md.update(dataBytes, 0, nread);
29

  
30
            byte[] mdbytes = md.digest();
31
            return toHexString(mdbytes);
32
        } catch (IOException e) {
33
            Log.e("FDroid", "Error reading \"" + apk.getAbsolutePath() + "\" to compute SHA1 hash.");
34
            return null;
35
        } catch (NoSuchAlgorithmException e) {
36
            Log.e("FDroid", "Device does not support " + algo + " MessageDisgest algorithm");
37
            return null;
38
        } finally {
39
            if (fis != null)
40
                try {
41
                    fis.close();
42
                } catch (IOException e) {
43
                    return null;
44
                }
45
        }
46
    }
47

  
48
    /**
49
     * Computes the base 16 representation of the byte array argument.
50
     *
51
     * @param bytes an array of bytes.
52
     * @return the bytes represented as a string of hexadecimal digits.
53
     */
54
    public static String toHexString(byte[] bytes) {
55
        BigInteger bi = new BigInteger(1, bytes);
56
        return String.format("%0" + (bytes.length << 1) + "X", bi);
57
    }
58

  
59
}

Also available in: Unified diff