LayoutInflater support andrroid:id (perfectgit status)

This commit is contained in:
houzh 2022-01-23 07:57:09 +00:00
parent d14df04bc4
commit 10d5924ef8
18 changed files with 181 additions and 118 deletions

View File

@ -22,7 +22,7 @@ add_custom_target(uidemopo
COMMENT "xgettext:Make MultiLanguage files (.po) for files in DIR:${PROJECT_SOURCE_DIR}"
)
CreatePAK( ${PROJECT_SOURCE_DIR}/assets ${PROJECT_BINARY_DIR}/uidemo.pak uidemo)
CreatePAK(uidemo ${PROJECT_SOURCE_DIR}/assets ${PROJECT_BINARY_DIR}/uidemo.pak ${PROJECT_SOURCE_DIR}/R.h)
add_custom_target(uidemotranslate
COMMAND python ${CMAKE_SOURCE_DIR}/src/tools/po2json.py ${CMAKE_CURRENT_BINARY_DIR}/uidemo.po

17
apps/uidemo/R.h Normal file
View File

@ -0,0 +1,17 @@
#pragma once
/*Generated by machine ,Do not edit!!!*/
namespace uidemo{
class R{
public:
enum id {
tablayout = 0x00002710 ,
viewpager = 0x00002711
};//endof enum id
};//endof class R
}//endof namespace

View File

@ -18,7 +18,7 @@
>
</ImageButton>
<TabLayout
android:id="200"
android:id="@id/tablayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="transparent"
@ -35,7 +35,7 @@
</ImageButton>
</LinearLayout>
<androidx.viewpager.widget.ViewPager
android:id="400"
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?><!--Generated by CDdroid's machine,Do not edit !!!-->
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<id name="tablayout">0x00002710</id>
<id name="viewpager">0x00002711</id>
</resources>

View File

@ -10,7 +10,7 @@
#include <string.h>
#include <core/textutils.h>
#include <fileadapter.h>
#include <R.h>
class FileTypeAdapter:public PagerAdapter{
public:
int getCount()override{return 3;}
@ -71,9 +71,9 @@ public:
MediaWindow::MediaWindow(int x,int y,int w,int h):Window(x,y,w,h){
ViewGroup*vg=(ViewGroup*)LayoutInflater::from(getContext())->inflate("layout/main.xml",this);
mAdapter=new FileTypeAdapter();
mTabLayout=(TabLayout*)vg->findViewById(200);
mPager = (ViewPager*)vg->findViewById(400);
mTabLayout=(TabLayout*)vg->findViewById(uidemo::R::id::tablayout);
mPager = (ViewPager*)vg->findViewById(uidemo::R::id::viewpager);
mTabLayout->setSelectedTabIndicatorColor(0x8000FF00);
mTabLayout->setSelectedTabIndicatorHeight(4);
mTabLayout->setTabIndicatorGravity(Gravity::BOTTOM);//TOP/BOTTOM/CENTER_VERTICAL/FILL_VERTICAL

View File

@ -1,9 +1,10 @@
function(CreatePAK ResourceDIR PakPath projectname)
add_custom_target(${projectname}_res
function(CreatePAK project ResourceDIR PakPath rhpath)
add_custom_target(${project}_Resource
COMMAND python ${CMAKE_SOURCE_DIR}/scripts/idgen.py ${ResourceDIR} ${rhpath}
COMMAND zip -r -0 ${PakPath} ./
WORKING_DIRECTORY ${ResourceDIR})
add_dependencies(${projectname} ${projectname}_res)
add_dependencies(${project} ${project}_Resource)
message("Package ${ResourceDIR} to:${PakPath}")
install(FILES ${PakPath} DESTINATION data)
endfunction()

View File

@ -2,6 +2,7 @@
import xml.sax
import os
import sys
import time
class CDROIDHandler( xml.sax.ContentHandler ):
def __init__(self):
@ -16,18 +17,17 @@ class CDROIDHandler( xml.sax.ContentHandler ):
pos=name.find('/');
if pos<0:
return
name=name[pos+1:]
if name[0].isalpha() or (name[0]=='_'):
self.idlist.append(name)
pos=name.find('.xml')
if pos>0:
name=name[pos+1:].strip()
if (name[0].isalpha() or (name[0]=='_')) and (name not in self.idlist) :
self.idlist.append(name)
def groupby(self):
new_ids = []
for id in self.idlist:
if id not in new_ids:
new_ids.append(id)
self.idlist=new_ids
print new_ids
return new_ids
class IDGenerater(object):
@ -39,17 +39,18 @@ class IDGenerater(object):
self.Handler = CDROIDHandler()
self.parser.setContentHandler( self.Handler )
def dict2RH(self,filepath):
def dict2RH(self,filepath,namespace):
fr=open(filepath,"w")
fr.write("#pragma once\n\n")
fr.write("/*Generated by machine ,Do not edit!!!*/\n\n")
fr.write("namespace cdroid{\n\n")
fr.write("namespace %s{\n\n"%(namespace))
fr.write("class R{\npublic:\n")
fr.write("%4senum id {\n"%(''))
dsize =len(self.Handler.idlist)
i=0
print self.Handler.idlist
for k in self.Handler.idlist:
fr.write("%8s%-16s= 0x%08X"%('',k,self.idstart+i))
fr.write("%8s%-24s= 0x%08X"%('',k,self.idstart+i))
if(i<dsize-1):
fr.write(" ,")
fr.write("\n")
@ -83,18 +84,28 @@ class IDGenerater(object):
fname=os.path.join(top, item)
if item.find('.xml')<0 or self.dirHasId(fname)<0:
continue
newestdate=os.stat(fname);
self.parser.parse(fname)
#print self.Handler.groupby()
if ( __name__ == "__main__"):
idstart=1000
idstart=10000
if len(sys.argv)<3:
print sys.argv[0]#+'assetspath R.h_path ID.xml'
if len(sys.argv)>3:
idstart=int(sys.argv[3])
validassetsdir=['assets','res','resources']
segments=sys.argv[1].split('/')
for i in range(0,len(segments)):
if segments[i] in validassetsdir:
break
namespace=segments[i]
if sys.argv[2].find("widget/R.h")>=0 and (namespace=='gui'):
namespace='cdroid'
idstart=1000
print "namespace="+namespace
idgen=IDGenerater(idstart)
idgen.scanxml(sys.argv[1])
idgen.dict2RH(sys.argv[2])
idgen.dict2RH(sys.argv[2],namespace)
if not os.path.exists(sys.argv[1]+"/values"):
os.makedirs(sys.argv[1]+"/values")
idgen.dict2ID(sys.argv[1]+"/values/ID.xml")

View File

@ -57,7 +57,7 @@ add_custom_target(systempak
COMMENT "package system resource"
)
CreatePAK(${PROJECT_SOURCE_DIR}/res ${PROJECT_BINARY_DIR}/cdroid.pak gui)
CreatePAK(gui ${PROJECT_SOURCE_DIR}/res ${PROJECT_BINARY_DIR}/cdroid.pak ${PROJECT_SOURCE_DIR}/widget/R.h)
set_target_properties(gui_static PROPERTIES OUTPUT_NAME "gui")
file(GLOB_RECURSE allfiles RELATIVE "${PROJECT_SOURCE_DIR}/" "*.h")

View File

@ -16,7 +16,6 @@ namespace cdroid{
Assets::Assets(){
addResource("cdroid.pak","cdroid");
getId("homescreen");
}
Assets::Assets(const std::string&path):Assets(){
@ -45,6 +44,7 @@ int Assets::addResource(const std::string&path,const std::string&name){
std::vector<std::string>entries;
pak->getEntries(entries);
LOGD("entries.count=%d pakpath=%s",entries.size(),path.c_str());
fetchIdFromResource(name+":values/ID.xml");
return pak?0:-1;
}
@ -61,32 +61,42 @@ static bool guessExtension(ZIPArchive*pak,std::string&ioname){
return ret;
}
//"@android:drawable/ic_dialog_email"
//"@drawable/test"
ZIPArchive*Assets::getResource(const std::string&fullResId,std::string*relativeResid)const{
std::string pakName=mName;
size_t pos=fullResId.find(':');
//"@[+][package:]id/filname"
void Assets::parseResource(const std::string&fullResId,std::string*res,std::string*ns)const{
std::string pkg=mName;
size_t pos=fullResId.find(":");
std::string relname;
if(pos!=std::string::npos){
int startat=(fullResId.find('@')==std::string::npos)?0:1;
pakName=fullResId.substr(startat,pos-startat);
const int pluspos=fullResId.find('+');
const int atpos=fullResId.find('@');
const int startat=(pluspos!=std::string::npos)?pluspos:((atpos==std::string::npos)?0:atpos);
pkg=fullResId.substr(startat,pos-startat);
relname=fullResId.substr(pos+1);
}else{
}else{//@+id/
pos=mName.find_last_of('/');
if(pos!=std::string::npos)
pakName=mName.substr(pos+1);
pos=fullResId.find('@');
pkg=mName.substr(pos+1);
pos=fullResId.find('+');
if(pos==std::string::npos)
pos=fullResId.find('@');
if(pos!=std::string::npos)relname=fullResId.substr(pos+1);
else relname=fullResId;
}
auto it=mResources.find(pakName);
if(res)*res=relname;
if(ns)*ns=pkg;
}
ZIPArchive*Assets::getResource(const std::string&fullResId,std::string*relativeResid)const{
std::string package,resname;
parseResource(fullResId,&resname,&package);
auto it=mResources.find(package);
ZIPArchive*pak=nullptr;
if(it!=mResources.end()){//convert noextname ->extname.
pak=it->second;
guessExtension(pak,relname);
if(relativeResid) *relativeResid=relname;
guessExtension(pak,resname);
if(relativeResid) *relativeResid=resname;
}
LOGV_IF(relname.size(),"resource for [%s::%s:%s] is%s found",pakName.c_str(),fullResId.c_str(),relname.c_str(),(pak?"":" not"));
LOGV_IF(resname.size(),"resource for [%s::%s:%s] is%s found",package.c_str(),fullResId.c_str(),resname.c_str(),(pak?"":" not"));
return pak;
}
@ -98,6 +108,25 @@ std::unique_ptr<std::istream> Assets::getInputStream(const std::string&fullresid
return is;
}
int Assets::fetchIdFromResource(const std::string&fullresid){
int count=0;
std::string package;
parseResource(fullresid,nullptr,&package);
package+=":id/";
auto func=[&](const std::string&section,const AttributeSet*att1,
const AttributeSet&att2,const std::string&value,int index){
if(section.length()&&section[0]=='i'&&section[1]=='d'){
const std::string name=package+att2.getString("name");
mIDS[name]=TextUtils::strtol(value);
count++;
LOG(DEBUG)<<name<<"-->"<<value;
}
};
loadKeyValues(fullresid,func);
LOGD("load %d ids from %s",count,fullresid.c_str());
return count;
}
void Assets::loadStrings(const std::string&lan){
const std::string fname="strings/strings-"+lan+".json";
Json::CharReaderBuilder builder;
@ -135,16 +164,14 @@ RefPtr<ImageSurface>Assets::getImage(const std::string&fullresid){
return img;
}
int Assets::getId(const std::string&key){
auto func=[&](const std::string&section,const AttributeSet*att1,
const AttributeSet&att2,const std::string&value,int index){
if(section.length()&&section[0]=='i'&&section[1]=='d'){
const std::string name=att2.getString("name");
mIDS[name]=TextUtils::strtol(value);
LOG(DEBUG)<<name<<"========>>>>>"<<value;
}
};
loadKeyValues("cdroid:xml/ID.xml",func);
int Assets::getId(const std::string&key)const{
std::string resid,pkg;
if(key.empty())return -1;
if(key.length()&&(key.find('/')==std::string::npos))
return TextUtils::strtol(key);
parseResource(key,&resid,&pkg);
auto it=mIDS.find(pkg+":"+resid);
return it==mIDS.end()?-1:it->second;
}
const std::string& Assets::getString(const std::string& id,const std::string&lan){
@ -279,11 +306,11 @@ int Assets::loadKeyValues(const std::string&fullresid,std::function<void(const s
const AttributeSet*,const AttributeSet&,const std::string&,int)>func){
int len = 0;
char buf[256];
std::string resname;
ZIPArchive*pak=getResource(fullresid,&resname);
void*zfile=pak?pak->getZipHandle(resname):nullptr;
ZipInputStream stream(zfile);
std::unique_ptr<std::istream>stream=getInputStream(fullresid);
LOGE_IF(stream==nullptr,"%s load failed",fullresid.c_str());
if(stream==nullptr)
return 0;
XML_Parser parser=XML_ParserCreate(nullptr);
std::string curKey;
std::string curValue;
@ -293,8 +320,8 @@ int Assets::loadKeyValues(const std::string&fullresid,std::function<void(const s
XML_SetElementHandler(parser, startElement, endElement);
XML_SetCharacterDataHandler(parser,CharacterHandler);
do {
stream.read(buf,sizeof(buf));
len=stream.gcount();
stream->read(buf,sizeof(buf));
len=stream->gcount();
if (XML_Parse(parser, buf,len,len==0) == XML_STATUS_ERROR) {
const char*es=XML_ErrorString(XML_GetErrorCode(parser));
LOGE("%s at line %ld",es, XML_GetCurrentLineNumber(parser));

View File

@ -17,7 +17,9 @@ private:
std::map<const std::string,std::weak_ptr<Drawable::ConstantState>>mDrawables;
std::map<const std::string,class ZIPArchive*>mResources;
std::map<const std::string,AttributeSet>mStyles;
void parseResource(const std::string&fullresid,std::string*res,std::string*ns)const;
ZIPArchive*getResource(const std::string & fullresid, std::string* relativeResid)const;
int fetchIdFromResource(const std::string&fullresid);
protected:
std::string mName;
DisplayMetrics mDisplayMetrics;
@ -33,7 +35,7 @@ public:
int loadStyles(const std::string&resid);
void clearStyles();
const DisplayMetrics&getDisplayMetrics()override;
int getId(const std::string&)override;
int getId(const std::string&)const override;
const std::string& getString(const std::string&id,const std::string&lan="")override;
RefPtr<Cairo::ImageSurface> getImage(const std::string&resname)override;
std::vector<std::string> getStringArray(const std::string&resname,const std::string&arrayname)const;

View File

@ -19,7 +19,7 @@ class ColorStateList;
class Context{
public:
virtual const DisplayMetrics&getDisplayMetrics()=0;
virtual int getId(const std::string&)=0;
virtual int getId(const std::string&)const=0;
virtual const std::string& getString(const std::string&id,const std::string&lan="")=0;
static RefPtr<Cairo::ImageSurface> loadImage( std::istream&istream ){
return Cairo::ImageSurface::create_from_stream(istream);

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:cdroid="http://schemas.android.com/apk/res/android">
<item cdroid:id="0">
<item cdroid:id="@+cdroid:id/background">
<shape cdroid:shape="rectangle">
<corners cdroid:radius="5dip" />
<gradient
@ -14,7 +14,7 @@
</shape>
</item>
<item cdroid:id="1">
<item cdroid:id="@+cdroid:id/progress">
<clip>
<shape cdroid:shape="rectangle">
<corners cdroid:radius="5dip" />
@ -29,7 +29,7 @@
</clip>
</item>
<item cdroid:id="2">
<item cdroid:id="@+cdroid:id/secondaryProgress">
<clip>
<shape cdroid:shape="rectangle">
<corners cdroid:radius="5dip" />

View File

@ -1,6 +1,6 @@
<layer-list xmlns:cdroid="http://schemas.android.com/apk/res/android">
<item cdroid:id="0">
<item cdroid:id="@+cdroid:id/background">
<shape cdroid:shape="rectangle">
<corners cdroid:radius="5dip" />
<gradient
@ -13,7 +13,7 @@
</shape>
</item>
<item cdroid:id="1">
<item cdroid:id="@+cdroid:id/progress">
<clip cdroid:clipOrientation="vertical" cdroid:gravity = "bottom">
<shape cdroid:shape="rectangle">
<corners cdroid:radius="5dip" />
@ -28,7 +28,7 @@
</clip>
</item>
<item cdroid:id="2">
<item cdroid:id="@+cdroid:id/secondaryProgress">
<clip cdroid:clipOrientation="vertical"
cdroid:gravity = "bottom">
<shape cdroid:shape="rectangle">

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:cdroid="http://schemas.android.com/apk/res/android">
<ImageButton cdroid:id="1@+id/increment"
<ImageButton cdroid:id="@+cdroid:id/increment"
cdroid:layout_width="fill_parent"
cdroid:layout_height="wrap_content"
cdroid:background="cdroid:drawable/numberpicker_up_btn"
@ -8,14 +8,14 @@
cdroid:paddingBottom="22dip"
cdroid:contentDescription="@string/number_picker_increment_button" />
<EditText
cdroid:id="0@+id/numberpicker_input"
cdroid:id="@+cdroid:id/numberpicker_input"
cdroid:layout_width="fill_parent"
cdroid:layout_height="wrap_content"
cdroid:textAppearance="@style/TextAppearance.Large.Inverse.NumberPickerInputText"
cdroid:gravity="center"
cdroid:singleLine="true"
cdroid:background="cdroid:drawable/numberpicker_input" />
<ImageButton cdroid:id="2@+id/decrement"
<ImageButton cdroid:id="@+cdroid:id/decrement"
cdroid:layout_width="fill_parent"
cdroid:layout_height="wrap_content"
cdroid:background="cdroid:drawable/numberpicker_down_btn"

View File

@ -7,19 +7,21 @@
<id name="parentPanel">0x000003ec</id>
<id name="topPanel">0x000003ed</id>
<id name="title_template">0x000003ee</id>
<id name="icon">0x000003ef</id>
<id name="alertTitle">0x000003f0</id>
<id name="titleDivider">0x000003f1</id>
<id name="contentPanel">0x000003f2</id>
<id name="scrollView">0x000003f3</id>
<id name="message">0x000003f4</id>
<id name="customPanel">0x000003f5</id>
<id name="custom">0x000003f6</id>
<id name="buttonPanel">0x000003f7</id>
<id name="leftSpacer">0x000003f8</id>
<id name="button1">0x000003f9</id>
<id name="button3">0x000003fa</id>
<id name="button2">0x000003fb</id>
<id name="rightSpacer">0x000003fc</id>
<id name="alertTitle">0x000003ef</id>
<id name="titleDivider">0x000003f0</id>
<id name="contentPanel">0x000003f1</id>
<id name="scrollView">0x000003f2</id>
<id name="message">0x000003f3</id>
<id name="customPanel">0x000003f4</id>
<id name="custom">0x000003f5</id>
<id name="buttonPanel">0x000003f6</id>
<id name="leftSpacer">0x000003f7</id>
<id name="button1">0x000003f8</id>
<id name="button3">0x000003f9</id>
<id name="button2">0x000003fa</id>
<id name="rightSpacer">0x000003fb</id>
<id name="background">0x000003fc</id>
<id name="progress">0x000003fd</id>
<id name="secondaryProgress">0x000003fe</id>
</resources>

View File

@ -1,41 +1,38 @@
#ifndef __CDROID_R_H__
#define __CDROID_R_H__
#pragma once
/*Generated by machine ,Do not edit!!!*/
namespace cdroid{
class R{
public:
enum id{
background=0,
progress,
secondaryProgress,
toggle,
text1,
numberpicker_input,//for numberpicker
increment,//for numberpicker
decrement,//for numberpicker
/**for dialog */
parentPanel,
topPanel,
contentPanel,
buttonPanel,
customPanel,
custom,
scrollView,
message,
leftSpacer,
button1,
button2,
button3,
rightSpacer,
enum id {
increment = 0x000003E8 ,
numberpicker_input = 0x000003E9 ,
decrement = 0x000003EA ,
icon = 0x000003EB ,
parentPanel = 0x000003EC ,
topPanel = 0x000003ED ,
title_template = 0x000003EE ,
alertTitle = 0x000003EF ,
titleDivider = 0x000003F0 ,
contentPanel = 0x000003F1 ,
scrollView = 0x000003F2 ,
message = 0x000003F3 ,
customPanel = 0x000003F4 ,
custom = 0x000003F5 ,
buttonPanel = 0x000003F6 ,
leftSpacer = 0x000003F7 ,
button1 = 0x000003F8 ,
button3 = 0x000003F9 ,
button2 = 0x000003FA ,
rightSpacer = 0x000003FB ,
background = 0x000003FC ,
progress = 0x000003FD ,
secondaryProgress = 0x000003FE
};//endof enum id
};//endof class R
}//endof namespace
textSpacerNoButtons,
titleDividerNoCustom,
titleDivider,
titleDividerTop,
textSpacerNoTitle,
title_template,
icon,
alertTitle,
};
};
}
#endif

View File

@ -1016,7 +1016,7 @@ void TabLayout::TabView::update() {
mIconView->setImageDrawable(nullptr);
}
mCustomTextView = (TextView*) custom->findViewById(cdroid::R::id::text1);
//mCustomTextView = (TextView*) custom->findViewById(cdroid::R::id::text1);
if (mCustomTextView != nullptr) {
mDefaultMaxLines = mCustomTextView->getMaxLines();// TextViewCompat.getMaxLines(mCustomTextView);
}

View File

@ -152,7 +152,7 @@ View::View(Context*ctx,const AttributeSet&attrs){
initView();
mContext=ctx;
mID = attrs.getInt("id",NO_ID);
mID = ctx->getId(attrs.getString("id"));
mMinWidth = attrs.getDimensionPixelSize("minWidth",0);
mMinHeight= attrs.getDimensionPixelSize("minHeight",0);
mContentDescription=attrs.getString("contentDescription");