120bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood/*
220bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood * Copyright (C) 2010 The Android Open Source Project
320bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood *
420bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood * Licensed under the Apache License, Version 2.0 (the "License");
520bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood * you may not use this file except in compliance with the License.
620bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood * You may obtain a copy of the License at
720bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood *
820bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood *      http://www.apache.org/licenses/LICENSE-2.0
920bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood *
1020bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood * Unless required by applicable law or agreed to in writing, software
1120bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood * distributed under the License is distributed on an "AS IS" BASIS,
1220bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1320bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood * See the License for the specific language governing permissions and
1420bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood * limitations under the License.
1520bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood */
1620bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood
1720bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwoodpackage com.android.camerabrowser;
1820bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood
1920bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwoodimport android.app.ListActivity;
2020bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwoodimport android.content.Context;
2120bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwoodimport android.content.Intent;
2220bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwoodimport android.mtp.MtpDevice;
2320bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwoodimport android.mtp.MtpStorageInfo;
2420bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwoodimport android.os.Bundle;
2520bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwoodimport android.util.Log;
2620bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwoodimport android.view.LayoutInflater;
2720bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwoodimport android.view.View;
2820bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwoodimport android.view.ViewGroup;
2920bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwoodimport android.widget.BaseAdapter;
3020bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwoodimport android.widget.ListView;
3120bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwoodimport android.widget.TextView;
3220bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood
3320bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwoodimport java.util.List;
3420bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood
3520bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood/**
3620bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood * A list view displaying all storage units on a device.
3720bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood */
3820bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwoodpublic class StorageBrowser extends ListActivity {
3920bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood
4020bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood    private static final String TAG = "StorageBrowser";
4120bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood
4220bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood    private MtpClient mClient;
4320bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood    private String mDeviceName;
4420bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood    private List<MtpStorageInfo> mStorageList;
4520bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood    private DeviceDisconnectedReceiver mDisconnectedReceiver;
4620bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood
4720bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood    private class StorageAdapter extends BaseAdapter {
4820bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood        private final Context mContext;
4920bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood        private final LayoutInflater mInflater;
5020bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood
5120bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood        public StorageAdapter(Context c) {
5220bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood            mContext = c;
5320bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood            mInflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
5420bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood        }
5520bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood
5620bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood        public int getCount() {
5720bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood            if (mStorageList == null) {
5820bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood                return 0;
5920bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood            } else {
6020bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood                return mStorageList.size();
6120bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood            }
6220bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood        }
6320bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood
6420bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood        public Object getItem(int position) {
6520bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood            return mStorageList.get(position);
6620bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood        }
6720bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood
6820bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood        public long getItemId(int position) {
6920bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood            return position;
7020bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood        }
7120bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood
7220bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood        public View getView(int position, View convertView, ViewGroup parent) {
7320bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood            TextView view;
7420bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood            if (convertView == null) {
7520bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood                view = (TextView)mInflater.inflate(
7620bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood                        android.R.layout.simple_list_item_1, parent, false);
7720bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood            } else {
7820bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood                view = (TextView)convertView;
7920bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood            }
8020bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood
8120bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood            MtpStorageInfo info = mStorageList.get(position);
8220bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood            if (info != null) {
8320bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood                view.setText(info.getDescription());
8420bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood            } else {
8520bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood                view.setText("???");
8620bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood            }
8720bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood            return view;
8820bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood        }
8920bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood    }
9020bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood
9120bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood    @Override
9220bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood    protected void onCreate(Bundle savedInstanceState) {
9320bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood        super.onCreate(savedInstanceState);
9420bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood
9520bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood        mClient = ((CameraBrowserApplication)getApplication()).getMtpClient();
9620bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood        mDeviceName = getIntent().getStringExtra("device");
9720bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood        mDisconnectedReceiver = new DeviceDisconnectedReceiver(this, mDeviceName);
9820bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood    }
9920bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood
10020bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood    @Override
10120bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood    protected void onResume() {
10220bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood        super.onResume();
10320bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood        mStorageList = mClient.getStorageList(mDeviceName);
10420bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood        setListAdapter(new StorageAdapter(this));
10520bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood    }
10620bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood
10720bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood    @Override
10820bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood    protected void onDestroy() {
10920bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood        unregisterReceiver(mDisconnectedReceiver);
11020bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood        super.onDestroy();
11120bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood    }
11220bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood
11320bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood    @Override
11420bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood    protected void onListItemClick(ListView l, View v, int position, long id) {
11520bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood        Intent intent = new Intent(this, ObjectBrowser.class);
11620bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood        intent.putExtra("device", mDeviceName);
11720bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood        intent.putExtra("storage", mStorageList.get(position).getStorageId());
11820bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood        startActivity(intent);
11920bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood    }
12020bc97fc541610a296d12e25570ac3eb6d599673Mike Lockwood}
121