• 读ActiveAndroid源码(二)


      上一次粗略地读了一下TableInfo这个类,了解了一下ActiveAndroid中注解的使用方法,算是一个预热,这一篇,从正常的顺序开始。

      所以,这次从ActiveAndroid的初始化开始阅读。

    public class Application extends android.app.Application {
        @Override
        public void onCreate() {
            super.onCreate();
            ActiveAndroid.initialize(this);
        }
        
        @Override
        public void onTerminate() {
            super.onTerminate();
            ActiveAndroid.dispose();
        }
    }

      先看ActiveAndroid.initialize(this);

      

    public final class ActiveAndroid {
        //////////////////////////////////////////////////////////////////////////////////////
        // PUBLIC METHODS
        //////////////////////////////////////////////////////////////////////////////////////
    
        public static void initialize(Context context) {
            initialize(new Configuration.Builder(context).create());
        }
      ……
    }

      只能先看看Configuration这个类。

      

    public class Configuration {
    
        public final static String SQL_PARSER_LEGACY = "legacy";
        public final static String SQL_PARSER_DELIMITED = "delimited";
    ……
        public static class Builder {
            //////////////////////////////////////////////////////////////////////////////////////
            // PRIVATE CONSTANTS
            //////////////////////////////////////////////////////////////////////////////////////
    
            private static final String AA_DB_NAME = "AA_DB_NAME";
            private static final String AA_DB_VERSION = "AA_DB_VERSION";
            private final static String AA_MODELS = "AA_MODELS";
            private final static String AA_SERIALIZERS = "AA_SERIALIZERS";
            private final static String AA_SQL_PARSER = "AA_SQL_PARSER";
    
            private static final int DEFAULT_CACHE_SIZE = 1024;
            private static final String DEFAULT_DB_NAME = "Application.db";
            private static final String DEFAULT_SQL_PARSER = SQL_PARSER_LEGACY;
    
            //////////////////////////////////////////////////////////////////////////////////////
            // PRIVATE MEMBERS
            //////////////////////////////////////////////////////////////////////////////////////
    
            private Context mContext;
    
            private Integer mCacheSize;
            private String mDatabaseName;
            private Integer mDatabaseVersion;
            private String mSqlParser;
            private List<Class<? extends Model>> mModelClasses;
            private List<Class<? extends TypeSerializer>> mTypeSerializers;
    
            //////////////////////////////////////////////////////////////////////////////////////
            // CONSTRUCTORS
            //////////////////////////////////////////////////////////////////////////////////////
    
            public Builder(Context context) {
                mContext = context.getApplicationContext();
                mCacheSize = DEFAULT_CACHE_SIZE;
            }
            ……
            public Configuration create() {
                Configuration configuration = new Configuration(mContext);
                configuration.mCacheSize = mCacheSize;
    
                // Get database name from meta-data
                if (mDatabaseName != null) {
                    configuration.mDatabaseName = mDatabaseName;
                } else {
                    configuration.mDatabaseName = getMetaDataDatabaseNameOrDefault();
                }
    
                // Get database version from meta-data
                if (mDatabaseVersion != null) {
                    configuration.mDatabaseVersion = mDatabaseVersion;
                } else {
                    configuration.mDatabaseVersion = getMetaDataDatabaseVersionOrDefault();
                }
    
                // Get SQL parser from meta-data
                if (mSqlParser != null) {
                    configuration.mSqlParser = mSqlParser;
                } else {
                    configuration.mSqlParser = getMetaDataSqlParserOrDefault();
                }
                
                // Get model classes from meta-data
                if (mModelClasses != null) {
                    configuration.mModelClasses = mModelClasses;
                } else {
                    final String modelList = ReflectionUtils.getMetaData(mContext, AA_MODELS);
                    if (modelList != null) {
                        configuration.mModelClasses = loadModelList(modelList.split(","));
                    }
                }
    
                // Get type serializer classes from meta-data
                if (mTypeSerializers != null) {
                    configuration.mTypeSerializers = mTypeSerializers;
                } else {
                    final String serializerList = ReflectionUtils.getMetaData(mContext, AA_SERIALIZERS);
                    if (serializerList != null) {
                        configuration.mTypeSerializers = loadSerializerList(serializerList.split(","));
                    }
                }
    
                return configuration;
            }
    ……
    }

       其中,最关键的就是create()这个方法。

      我们来逐行分析:

      首先,configuration获得application的context和缓存大小,默认的缓存大小为1024;

                Configuration configuration = new Configuration(mContext);
                configuration.mCacheSize = mCacheSize;

      第二步,获得database的name;

                // Get database name from meta-data
                if (mDatabaseName != null) {
                    configuration.mDatabaseName = mDatabaseName;
                } else {
                    configuration.mDatabaseName = getMetaDataDatabaseNameOrDefault();
                }

      其中,

            // Meta-data methods
    
            private String getMetaDataDatabaseNameOrDefault() {
                String aaName = ReflectionUtils.getMetaData(mContext, AA_DB_NAME);
                if (aaName == null) {
                    aaName = DEFAULT_DB_NAME;
                }
    
                return aaName;
            }
        @SuppressWarnings("unchecked")
        public static <T> T getMetaData(Context context, String name) {
            try {
                final ApplicationInfo ai = context.getPackageManager().getApplicationInfo(context.getPackageName(),
                        PackageManager.GET_META_DATA);
    
                if (ai.metaData != null) {
                    return (T) ai.metaData.get(name);
                }
            }
            catch (Exception e) {
                Log.w("Couldn't find meta-data: " + name);
            }
    
            return null;
        }

      用java动态取出Androidmanifest中,关于数据库的信息。

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.dream.fishbonelsy.baseonlsy" >
    
        <application
            android:name=".MyApplication"
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <meta-data android:name="AA_DB_NAME" android:value="Longsiyang.db" />
            <meta-data android:name="AA_DB_VERSION" android:value="2" />
        ……

     通过以上代码获得了数据库的name:Longsiyang.db

      第三步获得数据库版本号;

                // Get database version from meta-data
                if (mDatabaseVersion != null) {
                    configuration.mDatabaseVersion = mDatabaseVersion;
                } else {
                    configuration.mDatabaseVersion = getMetaDataDatabaseVersionOrDefault();
                }

      与第二步原理相同,获得版本号。

      第四步获得

                // Get SQL parser from meta-data
                if (mSqlParser != null) {
                    configuration.mSqlParser = mSqlParser;
                } else {
                    configuration.mSqlParser = getMetaDataSqlParserOrDefault();
                }
            private String getMetaDataSqlParserOrDefault() {
                final String mode = ReflectionUtils.getMetaData(mContext, AA_SQL_PARSER);
                if (mode == null) {
                    return DEFAULT_SQL_PARSER;
                }
                return mode;
            }

      默认情况下,mode==null,所以

            public final static String SQL_PARSER_LEGACY = "legacy";
            private static final String DEFAULT_SQL_PARSER = SQL_PARSER_LEGACY;            

     至此,Conguration的默认初始化已经完成。总结一下,Conguration类存储了数据库的:缓存大小、数据库名称、数据库版本号、数据库parser。有了这些信息,我们就可以建数据库了。

    ===============================================================================================================================================

    public final class ActiveAndroid {
        //////////////////////////////////////////////////////////////////////////////////////
        // PUBLIC METHODS
        //////////////////////////////////////////////////////////////////////////////////////
    
        public static void initialize(Context context) {
            initialize(new Configuration.Builder(context).create());
        }
      ……
    }

      回到这里,我们继续读initialize方法。进入initialize

        public static synchronized void initialize(Configuration configuration) {
            if (sIsInitialized) {
                Log.v("ActiveAndroid already initialized.");
                return;
            }
    
            sContext = configuration.getContext();
            sModelInfo = new ModelInfo(configuration);
            sDatabaseHelper = new DatabaseHelper(configuration);
    
            // TODO: It would be nice to override sizeOf here and calculate the memory
            // actually used, however at this point it seems like the reflection
            // required would be too costly to be of any benefit. We'll just set a max
            // object size instead.
            sEntities = new LruCache<String, Model>(configuration.getCacheSize());
    
            openDatabase();
    
            sIsInitialized = true;
    
            Log.v("ActiveAndroid initialized successfully.");
        }

       在这里,将Conguration信息传入ModelInfo,并打开数据库,将标志位sIsInitialized打开。

       接下来的一篇,将继续阅读打开数据库的相关代码。

      Done!   

  • 相关阅读:
    使用git管理github项目
    router 跳转页面
    JS中[object object]怎么取值
    微信授权获取code
    闭包
    css属性clear
    javaScript循环
    css属性position
    跨域
    浅析JS内存 一
  • 原文地址:https://www.cnblogs.com/fishbone-lsy/p/4886802.html
Copyright © 2020-2023  润新知