summaryrefslogtreecommitdiffstats
path: root/src/com/android/gallery3d/data/MtpDeviceSet.java
blob: bc4bc6321362a705d8b297d10fef9e1a2403e0d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.gallery3d.data;

import android.annotation.TargetApi;
import android.mtp.MtpDeviceInfo;
import android.net.Uri;
import android.os.Handler;
import android.util.Log;

import com.android.gallery3d.R;
import com.android.gallery3d.app.GalleryApp;
import com.android.gallery3d.common.ApiHelper;
import com.android.gallery3d.util.Future;
import com.android.gallery3d.util.FutureListener;
import com.android.gallery3d.util.MediaSetUtils;
import com.android.gallery3d.util.ThreadPool.Job;
import com.android.gallery3d.util.ThreadPool.JobContext;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

// MtpDeviceSet -- MtpDevice -- MtpImage
@TargetApi(ApiHelper.VERSION_CODES.HONEYCOMB_MR1)
public class MtpDeviceSet extends MediaSet
        implements FutureListener<ArrayList<MediaSet>> {
    private static final String TAG = "MtpDeviceSet";

    private GalleryApp mApplication;
    private final ChangeNotifier mNotifier;
    private final MtpContext mMtpContext;
    private final String mName;
    private final Handler mHandler;

    private Future<ArrayList<MediaSet>> mLoadTask;
    private ArrayList<MediaSet> mDeviceSet = new ArrayList<MediaSet>();
    private ArrayList<MediaSet> mLoadBuffer;
    private boolean mIsLoading;

    public MtpDeviceSet(Path path, GalleryApp application, MtpContext mtpContext) {
        super(path, nextVersionNumber());
        mApplication = application;
        mNotifier = new ChangeNotifier(this, Uri.parse("mtp://"), application);
        mMtpContext = mtpContext;
        mName = application.getResources().getString(R.string.set_label_mtp_devices);
        mHandler = new Handler(mApplication.getMainLooper());
    }

    private class DevicesLoader implements Job<ArrayList<MediaSet>> {
        @Override
        public ArrayList<MediaSet> run(JobContext jc) {
            DataManager dataManager = mApplication.getDataManager();
            ArrayList<MediaSet> result = new ArrayList<MediaSet>();

            // Enumerate all devices
            List<android.mtp.MtpDevice> devices = mMtpContext.getMtpClient().getDeviceList();
            Log.v(TAG, "loadDevices: " + devices + ", size=" + devices.size());
            for (android.mtp.MtpDevice mtpDevice : devices) {
                synchronized (DataManager.LOCK) {
                    int deviceId = mtpDevice.getDeviceId();
                    Path childPath = mPath.getChild(deviceId);
                    MtpDevice device = (MtpDevice) dataManager.peekMediaObject(childPath);
                    if (device == null) {
                        device = new MtpDevice(childPath, mApplication, deviceId, mMtpContext);
                    }
                    Log.d(TAG, "add device " + device);
                    result.add(device);
                }
            }
            Collections.sort(result, MediaSetUtils.NAME_COMPARATOR);
            return result;
        }
    }

    public static String getDeviceName(MtpContext mtpContext, int deviceId) {
        android.mtp.MtpDevice device = mtpContext.getMtpClient().getDevice(deviceId);
        if (device == null) {
            return "";
        }
        MtpDeviceInfo info = device.getDeviceInfo();
        if (info == null) {
            return "";
        }
        String manufacturer = info.getManufacturer().trim();
        String model = info.getModel().trim();
        return manufacturer + " " + model;
    }

    @Override
    public MediaSet getSubMediaSet(int index) {
        return index < mDeviceSet.size() ? mDeviceSet.get(index) : null;
    }

    @Override
    public int getSubMediaSetCount() {
        return mDeviceSet.size();
    }

    @Override
    public String getName() {
        return mName;
    }

    @Override
    public synchronized boolean isLoading() {
        return mIsLoading;
    }

    @Override
    public synchronized long reload() {
        if (mNotifier.isDirty()) {
            if (mLoadTask != null) mLoadTask.cancel();
            mIsLoading = true;
            mLoadTask = mApplication.getThreadPool().submit(new DevicesLoader(), this);
        }
        if (mLoadBuffer != null) {
            mDeviceSet = mLoadBuffer;
            mLoadBuffer = null;
            for (MediaSet device : mDeviceSet) {
                device.reload();
            }
            mDataVersion = nextVersionNumber();
        }
        return mDataVersion;
    }

    @Override
    public synchronized void onFutureDone(Future<ArrayList<MediaSet>> future) {
        if (future != mLoadTask) return;
        mLoadBuffer = future.get();
        mIsLoading = false;
        if (mLoadBuffer == null) mLoadBuffer = new ArrayList<MediaSet>();

        mHandler.post(new Runnable() {
            @Override
            public void run() {
                notifyContentChanged();
            }
        });
    }
}