Browse Source

1:修改ClassUtils方法,增加多个路径下寻找类的方法;

guiying712 8 years ago
parent
commit
cf8cb5ca4c

+ 51 - 25
lib_common/src/main/java/com/guiying/module/common/base/BaseActivity.java

@@ -19,26 +19,6 @@ import com.guiying.module.common.utils.Utils;
  */
 public abstract class BaseActivity extends AppCompatActivity {
 
-    /**
-     * Setup the toolbar.
-     *
-     * @param toolbar   toolbar
-     * @param hideTitle 是否隐藏Title
-     */
-    protected void setupToolBar(Toolbar toolbar, boolean hideTitle) {
-        setSupportActionBar(toolbar);
-        ActionBar actionBar = getSupportActionBar();
-        if (actionBar != null) {
-            actionBar.setHomeAsUpIndicator(R.drawable.ic_arrow_back);
-            actionBar.setDisplayHomeAsUpEnabled(true);
-            actionBar.setDisplayShowHomeEnabled(true);
-            if (hideTitle) {
-                //隐藏Title
-                actionBar.setDisplayShowTitleEnabled(false);
-            }
-        }
-    }
-
 
     /**
      * 封装的findViewByID方法
@@ -48,6 +28,7 @@ public abstract class BaseActivity extends AppCompatActivity {
         return (T) super.findViewById(id);
     }
 
+
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -67,7 +48,34 @@ public abstract class BaseActivity extends AppCompatActivity {
         return true;
     }
 
-    //添加fragment
+
+    /**
+     * Setup the toolbar.
+     *
+     * @param toolbar   toolbar
+     * @param hideTitle 是否隐藏Title
+     */
+    protected void setupToolBar(Toolbar toolbar, boolean hideTitle) {
+        setSupportActionBar(toolbar);
+        ActionBar actionBar = getSupportActionBar();
+        if (actionBar != null) {
+            actionBar.setHomeAsUpIndicator(R.drawable.ic_arrow_back);
+            actionBar.setDisplayHomeAsUpEnabled(true);
+            actionBar.setDisplayShowHomeEnabled(true);
+            if (hideTitle) {
+                //隐藏Title
+                actionBar.setDisplayShowTitleEnabled(false);
+            }
+        }
+    }
+
+
+    /**
+     * 添加fragment
+     *
+     * @param fragment
+     * @param frameId
+     */
     protected void addFragment(BaseFragment fragment, @IdRes int frameId) {
         Utils.checkNotNull(fragment);
         getSupportFragmentManager().beginTransaction()
@@ -77,7 +85,12 @@ public abstract class BaseActivity extends AppCompatActivity {
 
     }
 
-    //替换fragment
+
+    /**
+     * 替换fragment
+     * @param fragment
+     * @param frameId
+     */
     protected void replaceFragment(BaseFragment fragment, @IdRes int frameId) {
         Utils.checkNotNull(fragment);
         getSupportFragmentManager().beginTransaction()
@@ -87,7 +100,11 @@ public abstract class BaseActivity extends AppCompatActivity {
 
     }
 
-    //隐藏fragment
+
+    /**
+     * 隐藏fragment
+     * @param fragment
+     */
     protected void hideFragment(BaseFragment fragment) {
         Utils.checkNotNull(fragment);
         getSupportFragmentManager().beginTransaction()
@@ -97,7 +114,10 @@ public abstract class BaseActivity extends AppCompatActivity {
     }
 
 
-    //显示fragment
+    /**
+     * 显示fragment
+     * @param fragment
+     */
     protected void showFragment(BaseFragment fragment) {
         Utils.checkNotNull(fragment);
         getSupportFragmentManager().beginTransaction()
@@ -107,6 +127,10 @@ public abstract class BaseActivity extends AppCompatActivity {
     }
 
 
+    /**
+     * 移除fragment
+     * @param fragment
+     */
     protected void removeFragment(BaseFragment fragment) {
         Utils.checkNotNull(fragment);
         getSupportFragmentManager().beginTransaction()
@@ -116,7 +140,9 @@ public abstract class BaseActivity extends AppCompatActivity {
     }
 
 
-    //移除fragment
+    /**
+     * 弹出栈顶部的Fragment
+     */
     protected void popFragment() {
         if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
             getSupportFragmentManager().popBackStack();

+ 5 - 5
lib_common/src/main/java/com/guiying/module/common/base/BaseApplication.java

@@ -13,7 +13,6 @@ import java.util.List;
  * 组件中实现的Application必须在debug包中的AndroidManifest.xml中注册,否则无法使用;
  * 组件的Application需置于java/debug文件夹中,不得放于主代码;
  * 组件中获取Context的方法必须为:Utils.getContext(),不允许其他写法;
- * BaseApplication主要用来管理全局Activity;
  *
  * @author 2016/12/2 17:02
  * @version V1.0.0
@@ -25,7 +24,7 @@ public class BaseApplication extends Application {
 
     private static BaseApplication sInstance;
 
-    private List<ApplicationDelegate> delegateList;
+    private List<ApplicationDelegate> mAppDelegateList;
 
 
     public static BaseApplication getIns() {
@@ -36,12 +35,13 @@ public class BaseApplication extends Application {
     public void onCreate() {
         super.onCreate();
         sInstance = this;
+        Logger.init("pattern").logLevel(LogLevel.FULL);
         Utils.init(this);
-        delegateList = ClassUtils.getObjectsWithInterface(this, ApplicationDelegate.class, ROOT_PACKAGE);
-        for (ApplicationDelegate delegate : delegateList) {
+        mAppDelegateList = ClassUtils.getObjectsWithInterface(this, ApplicationDelegate.class, ROOT_PACKAGE);
+        for (ApplicationDelegate delegate : mAppDelegateList) {
             delegate.onCreate();
         }
-        Logger.init("pattern").logLevel(LogLevel.FULL);
+
     }
 
 

+ 42 - 11
lib_common/src/main/java/com/guiying/module/common/base/BaseFragment.java

@@ -10,12 +10,6 @@ public abstract class BaseFragment extends Fragment {
 
     protected BaseActivity mActivity;
 
-
-    //获取宿主Activity
-    protected BaseActivity getHoldingActivity() {
-        return mActivity;
-    }
-
     @Override
     public void onAttach(Context context) {
         super.onAttach(context);
@@ -23,33 +17,68 @@ public abstract class BaseFragment extends Fragment {
     }
 
 
-    //添加fragment
+    /**
+     * 获取宿主Activity
+     *
+     * @return BaseActivity
+     */
+    protected BaseActivity getHoldingActivity() {
+        return mActivity;
+    }
+
+
+    /**
+     * 添加fragment
+     *
+     * @param fragment
+     * @param frameId
+     */
     protected void addFragment(BaseFragment fragment, @IdRes int frameId) {
         Utils.checkNotNull(fragment);
         getHoldingActivity().addFragment(fragment, frameId);
 
     }
 
-    //替换fragment
+
+    /**
+     * 替换fragment
+     *
+     * @param fragment
+     * @param frameId
+     */
     protected void replaceFragment(BaseFragment fragment, @IdRes int frameId) {
         Utils.checkNotNull(fragment);
         getHoldingActivity().replaceFragment(fragment, frameId);
     }
 
-    //隐藏fragment
+
+    /**
+     * 隐藏fragment
+     *
+     * @param fragment
+     */
     protected void hideFragment(BaseFragment fragment) {
         Utils.checkNotNull(fragment);
         getHoldingActivity().hideFragment(fragment);
     }
 
 
-    //显示fragment
+    /**
+     * 显示fragment
+     *
+     * @param fragment
+     */
     protected void showFragment(BaseFragment fragment) {
         Utils.checkNotNull(fragment);
         getHoldingActivity().showFragment(fragment);
     }
 
 
+    /**
+     * 移除Fragment
+     *
+     * @param fragment
+     */
     protected void removeFragment(BaseFragment fragment) {
         Utils.checkNotNull(fragment);
         getHoldingActivity().removeFragment(fragment);
@@ -57,7 +86,9 @@ public abstract class BaseFragment extends Fragment {
     }
 
 
-    //移除fragment
+    /**
+     * 弹出栈顶部的Fragment
+     */
     protected void popFragment() {
         getHoldingActivity().popFragment();
     }

+ 57 - 40
lib_common/src/main/java/com/guiying/module/common/base/ClassUtils.java

@@ -1,7 +1,5 @@
 package com.guiying.module.common.base;
 
-// Copy from galaxy sdk ${com.alibaba.android.galaxy.utils.ClassUtils}
-
 import android.content.Context;
 import android.content.SharedPreferences;
 import android.content.pm.ApplicationInfo;
@@ -9,7 +7,7 @@ import android.content.pm.PackageManager;
 import android.os.Build;
 import android.util.Log;
 
-import com.alibaba.android.arouter.launcher.ARouter;
+import com.guiying.module.common.utils.Utils;
 
 import java.io.File;
 import java.io.IOException;
@@ -23,17 +21,13 @@ import java.util.regex.Pattern;
 
 import dalvik.system.DexFile;
 
-import static com.alibaba.android.arouter.launcher.ARouter.logger;
-import static com.alibaba.android.arouter.utils.Consts.TAG;
-
 /**
+ * Copy from galaxy sdk ${com.alibaba.android.galaxy.utils.ClassUtils}
  * Scanner, find out class with any conditions, copy from google source code.
- *
- * @author 正纬 <a href="mailto:zhilong.liu@aliyun.com">Contact me.</a>
- * @version 1.0
- * @since 16/6/27 下午10:58
  */
 public class ClassUtils {
+    private static final String TAG = "ClassUtils";
+
     private static final String EXTRACTED_NAME_EXT = ".classes";
     private static final String EXTRACTED_SUFFIX = ".zip";
 
@@ -50,10 +44,19 @@ public class ClassUtils {
     }
 
 
+    /**
+     * 获取单一路径下所有实现了接口的类对象
+     *
+     * @param context U know
+     * @param clazz   接口
+     * @param path    包路径
+     * @param <T>     U know
+     * @return 对象列表
+     */
     public static <T> List<T> getObjectsWithInterface(Context context, Class<T> clazz, String path) {
         List<T> objectList = new ArrayList<>();
         try {
-            // These class was generate by arouter-compiler.
+            //找出所有路径中的类名,主要用于各个组件根包名一致的情况
             List<String> classFileNames = getFileNameByPackageName(context, path);
 
             for (String className : classFileNames) {
@@ -64,35 +67,48 @@ public class ClassUtils {
             }
 
             if (objectList.size() == 0) {
-                logger.error(TAG, "No files were found, check your configuration please!");
+                Log.e(TAG, "No files were found, check your configuration please!");
             }
         } catch (Exception e) {
             e.getStackTrace();
-            Log.e("ARouter", "getObjectsWithInterface error, " + e.getMessage());
+            Log.e(TAG, "getObjectsWithInterface error, " + e.getMessage());
         }
 
         return objectList;
     }
 
+
+    /**
+     * 获取多路径下所有实现了接口的类对象
+     *
+     * @param context  U know
+     * @param clazz    接口
+     * @param pathList 包路径列表
+     * @param <T>      U know
+     * @return 对象列表
+     */
     public static <T> List<T> getObjectsWithInterface(Context context, Class<T> clazz, List<String> pathList) {
         List<T> objectList = new ArrayList<>();
-//        try {
-//            // These class was generate by arouter-compiler.
-//            List<String> classFileNames = getFileNameByPackageName(context, path);
-//
-//            for (String className : classFileNames) {
-//                Class aClass = Class.forName(className);
-//                if (clazz.isAssignableFrom(aClass) && !clazz.equals(aClass) && !aClass.isInterface()) {
-//                    objectList.add((T) Class.forName(className).getConstructor().newInstance());
-//                }
-//            }
-//
-//            if (objectList.size() == 0) {
-//                logger.error(TAG, "No files were found, check your configuration please!");
-//            }
-//        } catch (Exception e) {
-//            e.getStackTrace();
-//        }
+        try {
+            for (String path : pathList) {
+                //找出所有路径中的类名,主要用于各个组件根包名不一致的情况
+                List<String> classFileNames = getFileNameByPackageName(context, path);
+
+                for (String className : classFileNames) {
+                    Class aClass = Class.forName(className);
+                    if (clazz.isAssignableFrom(aClass) && !clazz.equals(aClass) && !aClass.isInterface()) {
+                        objectList.add((T) Class.forName(className).getConstructor().newInstance());
+                    }
+                }
+            }
+
+            if (objectList.size() == 0) {
+                Log.e(TAG, "No files were found, check your configuration please!");
+            }
+        } catch (Exception e) {
+            e.getStackTrace();
+            Log.e(TAG, "getObjectsWithInterface error, " + e.getMessage());
+        }
 
         return objectList;
     }
@@ -125,7 +141,7 @@ public class ClassUtils {
                     }
                 }
             } catch (Throwable ignore) {
-                Log.e("ARouter", "Scan map file in dex files made error.", ignore);
+                Log.e(TAG, "Scan map file in dex files made error.", ignore);
             } finally {
                 if (null != dexfile) {
                     try {
@@ -136,7 +152,7 @@ public class ClassUtils {
             }
         }
 
-        Log.d("ARouter", "Filter " + classNames.size() + " classes by packageName <" + packageName + ">");
+        Log.d(TAG, "Filter " + classNames.size() + " classes by packageName <" + packageName + ">");
         return classNames;
     }
 
@@ -145,8 +161,8 @@ public class ClassUtils {
      *
      * @param context the application context
      * @return all the dex path
-     * @throws PackageManager.NameNotFoundException
-     * @throws IOException
+     * @throws PackageManager.NameNotFoundException Exception
+     * @throws IOException                          Exception
      */
     public static List<String> getSourcePaths(Context context) throws PackageManager.NameNotFoundException, IOException {
         ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), 0);
@@ -158,8 +174,8 @@ public class ClassUtils {
         //the prefix of extracted file, ie: test.classes
         String extractedFilePrefix = sourceApk.getName() + EXTRACTED_NAME_EXT;
 
-//        如果VM已经支持了MultiDex,就不要去Secondary Folder加载 Classesx.zip了,那里已经么有了
-//        通过是否存在sp中的multidex.version是不准确的,因为从低版本升级上来的用户,是包含这个sp配置的
+        //如果VM已经支持了MultiDex,就不要去Secondary Folder加载 Classesx.zip了,那里已经么有了
+        //通过是否存在sp中的multidex.version是不准确的,因为从低版本升级上来的用户,是包含这个sp配置的
         if (!isVMMultidexCapable()) {
             //the total dex numbers
             int totalDexNumber = getMultiDexPreferences(context).getInt(KEY_DEX_NUMBER, 1);
@@ -178,7 +194,8 @@ public class ClassUtils {
             }
         }
 
-        if (ARouter.debuggable()) { // Search instant run support only debuggable
+        if (Utils.isAppDebug()) {
+            // Search instant run support only debuggable
             sourcePaths.addAll(tryLoadInstantRunDexFile(applicationInfo));
         }
         return sourcePaths;
@@ -193,7 +210,7 @@ public class ClassUtils {
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && null != applicationInfo.splitSourceDirs) {
             // add the splite apk, normally for InstantRun, and newest version.
             instantRunSourcePaths.addAll(Arrays.asList(applicationInfo.splitSourceDirs));
-            Log.d("ARouter", "Found InstantRun support");
+            Log.d(TAG, "Found InstantRun support");
         } else {
             try {
                 // This man is reflection from Google instant run sdk, he will tell me where the dex files go.
@@ -209,11 +226,11 @@ public class ClassUtils {
                             instantRunSourcePaths.add(file.getAbsolutePath());
                         }
                     }
-                    Log.d("ARouter", "Found InstantRun support");
+                    Log.d(TAG, "Found InstantRun support");
                 }
 
             } catch (Exception e) {
-                Log.e("ARouter", "InstantRun support error, " + e.getMessage());
+                Log.e(TAG, "InstantRun support error, " + e.getMessage());
             }
         }
 

+ 1 - 1
lib_common/src/main/java/com/guiying/module/common/http/HttpClient.java

@@ -141,7 +141,7 @@ public class HttpClient {
                     try {
                         String result = response.body().string();
                         parseData(result, builder.clazz, builder.bodyType, onResultListener);
-                    } catch (IOException e) {
+                    } catch (IOException | IllegalStateException e) {
                         e.printStackTrace();
                     }
                 }