Cdroid/apps/uidemo1/multimedia.cc

160 lines
5.7 KiB
C++
Raw Normal View History

2022-01-18 16:21:54 +08:00
#include <cdroid.h>
2021-06-24 10:12:55 +08:00
#include <cdtypes.h>
#include <cdlog.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <fstream>
#include <string.h>
2021-08-24 09:39:38 +08:00
#include <core/textutils.h>
2022-01-21 11:13:32 +08:00
#include <fileadapter.h>
#include <R.h>
2022-01-21 11:13:32 +08:00
class FileTypeAdapter:public PagerAdapter{
private:
FileAdapter*adapter1 = nullptr;
FileAdapter*adapter2 = nullptr;
2021-06-24 10:12:55 +08:00
public:
~FileTypeAdapter(){
delete adapter1;
delete adapter2;
}
int getCount()override{return 5;}
2022-01-21 11:13:32 +08:00
bool isViewFromObject(View* view, void*object)override{ return view==object;}
void* instantiateItem(ViewGroup* container, int position)override{
std::string res[]={"@layout/layout1.xml","@layout/sweeptest.xml","@layout/layout3.xml"};
switch(position){
case 2:
case 3:
case 4:
{View*v=LayoutInflater::from(container->getContext())->inflate(res[position-2],nullptr,false);
container->addView(v);
v->requestLayout();
2022-09-22 15:34:29 +08:00
View*btn=v->findViewById(uidemo1::R::id::button1);
2022-03-09 17:55:57 +08:00
if(btn)btn->setOnClickListener([](View&v){
Window*w=new Window(0,0,400,400);
w->setBackgroundColor(0x80FF0000);
});
return v;
}break;
case 0:
{
ListView*lv=new ListView(800,480);
lv->setDivider(new ColorDrawable(0x80224422));
lv->setDividerHeight(1);
2022-07-06 09:46:10 +08:00
//lv->setFastScrollEnabled(true);
lv->setSelector(new ColorDrawable(0x8800FF00));
lv->setVerticalScrollBarEnabled(true);
lv->setOverScrollMode(View::OVER_SCROLL_ALWAYS);
2022-03-28 18:09:24 +08:00
if(adapter1==nullptr){
adapter1=new FileAdapter("@layout/fileitem.xml");
adapter1->loadFiles("/");
}
lv->setAdapter(adapter1);
lv->setBackgroundColor(0xFF000000|(0xFF<<position*8));
2022-03-14 21:38:21 +08:00
container->addView(lv).setId(12345);
lv->setOnItemClickListener([](AdapterView&lv,View&v,int pos,long id){
FileAdapter*adp=(FileAdapter*)lv.getAdapter();
2022-03-04 18:18:34 +08:00
FileItem f=adp->getItemAt(pos);
2023-01-06 14:10:56 +08:00
//LOG(DEBUG)<<"clicked "<<pos<<" "<<f.fileName;
if(f.isDir){
adp->clear();
adp->loadFiles(f.fullpath);
adp->notifyDataSetChanged();
2022-03-28 18:09:24 +08:00
lv.setSelection(0);
}
});
2023-01-06 14:10:56 +08:00
//LOGV("instantiateItem %d to %p",position,lv);
return lv;
}
2023-01-06 14:10:56 +08:00
case 1:{//LOGD("===========1111");
GridView*gv=new GridView(800,480);
2022-03-28 18:09:24 +08:00
if(adapter2==nullptr)
adapter2=new FileAdapter("@layout/fileitem2.xml");
2022-03-05 11:37:29 +08:00
gv->setOnItemClickListener([](AdapterView&lv,View&v,int pos,long id){
FileAdapter*adp=(FileAdapter*)lv.getAdapter();
FileItem f=adp->getItemAt(pos);
2023-01-06 14:10:56 +08:00
//LOG(DEBUG)<<"clicked "<<pos<<" "<<f.fileName;
2022-03-05 11:37:29 +08:00
if(f.isDir){
adp->clear();
adp->loadFiles(f.fullpath);
adp->notifyDataSetChanged();
2022-03-28 18:09:24 +08:00
lv.setSelection(0);
2022-03-05 11:37:29 +08:00
}
});
gv->setVerticalScrollBarEnabled(true);
gv->setScrollbarFadingEnabled(false);
2022-03-09 17:55:57 +08:00
gv->setNumColumns(2);
gv->setAdapter(adapter2);
gv->setHorizontalSpacing(2);
gv->setVerticalSpacing(2);
container->addView(gv).setId(12345);
adapter2->loadFiles("/");
adapter2->notifyDataSetChanged();
return gv;
2022-01-21 11:13:32 +08:00
}
}
2021-08-24 09:39:38 +08:00
}
2022-01-21 11:13:32 +08:00
void destroyItem(ViewGroup* container, int position,void* object)override{
container->removeView((View*)object);
2023-01-06 14:10:56 +08:00
//LOGV("destroyItem[%d]: %p",position,object);
delete (View*)object;
2021-08-24 09:39:38 +08:00
}
2022-01-21 11:13:32 +08:00
std::string getPageTitle(int position)override{
return std::string("Tab")+std::to_string(position);
2021-06-24 10:12:55 +08:00
}
2022-01-21 11:13:32 +08:00
float getPageWidth(int position)override{return 1.f;}//if returned calue <1 OffscreenPageLimit must be larger to workfine
2021-06-24 10:12:55 +08:00
};
2021-08-24 09:39:38 +08:00
2021-06-24 10:12:55 +08:00
class MediaWindow:public Window{
protected:
ListView*mdlist;
2022-01-21 11:13:32 +08:00
TabLayout*mTabLayout;
ViewPager*mPager;
2021-08-24 09:39:38 +08:00
TextView*mFilePath;
2022-01-21 11:13:32 +08:00
FileTypeAdapter*mAdapter;
2021-06-24 10:12:55 +08:00
std::string media_path;
HANDLE player;
Runnable run;
2021-06-24 10:12:55 +08:00
public:
MediaWindow(int x,int y,int w,int h);
~MediaWindow(){
delete mAdapter;
2021-06-24 10:12:55 +08:00
player=nullptr;
}
2021-06-24 10:12:55 +08:00
};
MediaWindow::MediaWindow(int x,int y,int w,int h):Window(x,y,w,h){
2022-01-21 11:13:32 +08:00
ViewGroup*vg=(ViewGroup*)LayoutInflater::from(getContext())->inflate("layout/main.xml",this);
mAdapter=new FileTypeAdapter();
2022-09-22 15:34:29 +08:00
mTabLayout=(TabLayout*)vg->findViewById(uidemo1::R::id::tablayout);
mPager = (ViewPager*)vg->findViewById(uidemo1::R::id::viewpager);
2022-01-21 11:13:32 +08:00
mTabLayout->setSelectedTabIndicatorColor(0x8000FF00);
mTabLayout->setSelectedTabIndicatorHeight(5);
2022-01-25 09:46:58 +08:00
mTabLayout->setTabTextColors(0xFFFF0000,0xFF00FF00);
2022-01-21 11:13:32 +08:00
mTabLayout->setTabIndicatorGravity(Gravity::BOTTOM);//TOP/BOTTOM/CENTER_VERTICAL/FILL_VERTICAL
2023-01-06 14:10:56 +08:00
//LOGD("pager=%p tab=%p this=%p:%p",mPager,mTabLayout,this,vg);
2022-01-21 11:13:32 +08:00
mPager->setAdapter(mAdapter);
mPager->setOffscreenPageLimit(1);//mAdapter->getCount());
mPager->setOverScrollMode(View::OVER_SCROLL_ALWAYS);
2022-01-21 11:13:32 +08:00
mTabLayout->setupWithViewPager(mPager);
mTabLayout->requestLayout();
run = [this](){
static int count=0;
2022-09-22 15:34:29 +08:00
TextView*tv=(TextView*)findViewById(uidemo1::R::id::light);
if(tv)tv->setText(std::to_string(count++));
postDelayed(run,1000);
};
//postDelayed(run,1000);
2021-06-24 10:12:55 +08:00
}
2021-08-24 09:39:38 +08:00
2021-06-24 10:12:55 +08:00
Window*CreateMultiMedia(){
2023-08-12 16:04:05 +08:00
MediaWindow*w=new MediaWindow(0,0,-1,-1);
w->setText("Media");
2023-01-06 14:10:56 +08:00
//LOGD("CreateMultiMedia");
2021-06-24 10:12:55 +08:00
return w;
}