Repository: zzhoujay/Gank4Android Branch: master Commit: d052b79c7c86 Files: 110 Total size: 268.2 KB Directory structure: gitextract_8uf00tm3/ ├── .gitignore ├── LICENSE ├── README.md ├── app/ │ ├── .gitignore │ ├── build.gradle │ ├── proguard-rules.pro │ └── src/ │ ├── androidTest/ │ │ └── java/ │ │ └── zhou/ │ │ └── gank/ │ │ └── io/ │ │ └── ApplicationTest.java │ └── main/ │ ├── AndroidManifest.xml │ ├── groovy/ │ │ └── zhou/ │ │ └── gank/ │ │ └── io/ │ │ ├── App.groovy │ │ ├── MainActivity.groovy │ │ ├── comment/ │ │ │ └── Config.groovy │ │ ├── data/ │ │ │ ├── CollectProvider.groovy │ │ │ ├── DataManager.groovy │ │ │ ├── DataProvider.groovy │ │ │ ├── RandomProvider.groovy │ │ │ ├── TimeProvider.groovy │ │ │ └── TypeProvider.groovy │ │ ├── database/ │ │ │ └── DatabaseManager.groovy │ │ ├── model/ │ │ │ ├── BaseResult.groovy │ │ │ ├── Bookmark.groovy │ │ │ ├── Gank.groovy │ │ │ ├── GankDaily.groovy │ │ │ ├── Result.groovy │ │ │ └── ResultDaily.groovy │ │ ├── net/ │ │ │ └── NetworkManager.groovy │ │ ├── ui/ │ │ │ ├── activity/ │ │ │ │ ├── CollectActivity.groovy │ │ │ │ ├── DailyActivity.groovy │ │ │ │ ├── HomeActivity.groovy │ │ │ │ ├── ImageGalleryActivity.groovy │ │ │ │ ├── SettingActivity.groovy │ │ │ │ ├── TabActivity.groovy │ │ │ │ ├── ToolbarActivity.groovy │ │ │ │ └── WebActivity.groovy │ │ │ ├── adapter/ │ │ │ │ ├── BaseAdapter.groovy │ │ │ │ ├── DailyAdapter.groovy │ │ │ │ ├── GankAdapter.groovy │ │ │ │ ├── ImageAdapter.groovy │ │ │ │ └── ImageGalleryAdapter.groovy │ │ │ ├── dialog/ │ │ │ │ └── InfoDialog.groovy │ │ │ ├── fragment/ │ │ │ │ ├── AdvanceFragment.groovy │ │ │ │ ├── BaseFragment.groovy │ │ │ │ ├── DailyFragment.groovy │ │ │ │ ├── GankFragment.groovy │ │ │ │ ├── ImageFragment.groovy │ │ │ │ ├── ImageGalleryFragment.groovy │ │ │ │ ├── ImagePageFragment.groovy │ │ │ │ ├── SettingFragment.groovy │ │ │ │ └── WebFragment.groovy │ │ │ └── weiget/ │ │ │ ├── SwipeToRefreshLayout.groovy │ │ │ ├── TouchImageView.java │ │ │ └── URLSpanNoUnderline.groovy │ │ └── util/ │ │ ├── FileKit.java │ │ ├── HashKit.java │ │ ├── JsonKit.groovy │ │ ├── LogKit.java │ │ ├── NetworkKit.groovy │ │ ├── Notifier.groovy │ │ ├── NumKit.groovy │ │ ├── Pageable.groovy │ │ ├── TextKit.groovy │ │ └── TimeKit.groovy │ └── res/ │ ├── drawable/ │ │ ├── ic_bookmark_48px.xml │ │ ├── ic_cloud_off_48px.xml │ │ ├── ic_cloud_queue_48px.xml │ │ ├── ic_dashboard_48px.xml │ │ ├── ic_event_48px.xml │ │ ├── ic_extension_48px.xml │ │ ├── ic_favorite_white_48px.xml │ │ ├── ic_info_48px.xml │ │ ├── ic_insert_emoticon_48px.xml │ │ ├── ic_menu_white_48px.xml │ │ ├── ic_refresh_48px.xml │ │ ├── ic_settings_black_48px.xml │ │ └── ic_view_module_48px.xml │ ├── layout/ │ │ ├── activity_compat.xml │ │ ├── activity_home.xml │ │ ├── activity_main.xml │ │ ├── activity_tab.xml │ │ ├── activity_toolbar.xml │ │ ├── dialog_text.xml │ │ ├── fragment_daily.xml │ │ ├── fragment_image_page.xml │ │ ├── fragment_recyler_view.xml │ │ ├── fragment_viewpager.xml │ │ ├── fragment_web.xml │ │ ├── item_daily.xml │ │ ├── item_gank.xml │ │ ├── item_image.xml │ │ ├── layout_gallery.xml │ │ ├── layout_more.xml │ │ └── nav_header.xml │ ├── menu/ │ │ ├── drawer_view.xml │ │ ├── menu_pop.xml │ │ ├── menu_pop_add.xml │ │ ├── menu_pop_remove.xml │ │ └── menu_web.xml │ ├── values/ │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ ├── values-en/ │ │ └── strings.xml │ ├── values-v21/ │ │ └── styles.xml │ └── xml/ │ └── setting.xml ├── build.gradle ├── gradle/ │ └── wrapper/ │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradle.properties ├── gradlew ├── gradlew.bat └── settings.gradle ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ .gradle /local.properties .DS_Store /build /captures .idea *.iml # built application files *.apk *.ap_ !/apk/app-release* # files for the dex VM *.dex # Java class files *.class # generated files bin/ gen/ # Eclipse project files .classpath .project # Proguard folder generated by Eclipse proguard/ # Intellij project files *.ipr *.iws ================================================ FILE: LICENSE ================================================ Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: You must give any other recipients of the Work or Derivative Works a copy of this License; and You must cause any modified files to carry prominent notices stating that You changed the files; and You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "{}" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright 2015 zzhoujay Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ================================================ FILE: README.md ================================================ #Gank4Android > Deprecated 推荐使用更加简介的[DailyGank](https://github.com/zzhoujay/DailyGank) > 使用`Groovy`开发的`Gank.IO` `Android`客户端 > > 遵循Google `Material Design`设计原则 ### 提示 由于Groovy的效率问题,和由于Groovy引入的一系列问题,本项目将不再推荐使用,推荐使用[DailyGank](https://github.com/zzhoujay/DailyGank)来阅读每天的干货,DailyGank使用Kotlin开发, 并且设计得十分的简洁,比本项目更快更轻量,推荐使用 **本项目将不再维护** ### 关于本项目 * 使用Groovy语言编写,使用[`groovy-android-gradle-plugin`](https://github.com/groovy/groovy-android-gradle-plugin)构建 * 项目中全面使用svg矢量图,感谢[`BetterVectorDrawable`](https://github.com/a-student/BetterVectorDrawable) * 对应GitHub地址: ### release版本 由于github的release中上传比较慢可以到[此处下载](http://www.pgyer.com/gank4android) ### 截图 ![运行截图](http://git.oschina.net/uploads/images/2015/0929/220758_085c1eb1_141009.jpeg "运行截图") ![运行截图](http://git.oschina.net/uploads/images/2015/0929/220831_147fc0c6_141009.jpeg "运行截图") ![运行截图](http://git.oschina.net/uploads/images/2015/0929/220846_6352f286_141009.jpeg "运行截图") ![运行截图](http://git.oschina.net/uploads/images/2015/0929/220909_17bb9a0e_141009.jpeg "运行截图") ![运行截图](http://git.oschina.net/uploads/images/2015/0929/220925_e86328e7_141009.jpeg "运行截图") ![运行截图](http://git.oschina.net/uploads/images/2015/0929/220933_ca9446c7_141009.jpeg "运行截图") ![运行截图](http://git.oschina.net/uploads/images/2015/0929/220940_187a5e9b_141009.jpeg "运行截图") ![运行截图](http://git.oschina.net/uploads/images/2015/0929/220953_e16a8999_141009.jpeg "运行截图") ![运行截图](http://git.oschina.net/uploads/images/2015/0929/220959_513cdc48_141009.jpeg "运行截图") ![运行截图](http://git.oschina.net/uploads/images/2015/0929/221006_39429de7_141009.jpeg "运行截图") _by zzhoujay_ ================================================ FILE: app/.gitignore ================================================ /build ================================================ FILE: app/build.gradle ================================================ apply plugin: 'com.android.application' buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:1.3.0' classpath 'org.codehaus.groovy:gradle-groovy-android-plugin:0.3.6' } } apply plugin: 'groovyx.grooid.groovy-android' android { compileSdkVersion 22 buildToolsVersion "22.0.1" defaultConfig { applicationId "zhou.gank.io" minSdkVersion 15 targetSdkVersion 22 versionCode 3 versionName "1.11" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } project.androidGroovy { options { sourceCompatibility = '1.7' targetCompatibility = '1.7' } } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') compile 'com.android.support:design:22.2.1' compile 'com.android.support:appcompat-v7:22.2.1' compile 'com.android.support:cardview-v7:22.2.1' compile 'com.android.support:recyclerview-v7:22.2.1' compile 'org.codehaus.groovy:groovy:2.4.3:grooid' compile('org.codehaus.groovy:groovy-json:2.4.3') { transitive = false } compile 'com.squareup.picasso:picasso:2.5.2' compile 'com.jakewharton:butterknife:7.0.1' compile 'com.bettervectordrawable:lib:0.4' compile 'com.google.code.gson:gson:2.3.1' compile 'com.squareup.okhttp:okhttp:2.5.0' compile 'com.github.flavienlaurent.datetimepicker:library:0.0.2' compile 'zhou.widget:advanceadapter:1.0' } ================================================ FILE: app/proguard-rules.pro ================================================ # Add project specific ProGuard rules here. # By default, the flags in this file are appended to flags specified # in /Users/zhou/Library/Android/sdk/tools/proguard/proguard-android.txt # You can edit the include path and order by changing the proguardFiles # directive in build.gradle. # # For more details, see # http://developer.android.com/guide/developing/tools/proguard.html # Add any project specific keep options here: # If your project uses WebView with JS, uncomment the following # and specify the fully qualified class name to the JavaScript interface # class: #-keepclassmembers class fqcn.of.javascript.interface.for.webview { # public *; #} ================================================ FILE: app/src/androidTest/java/zhou/gank/io/ApplicationTest.java ================================================ package zhou.gank.io; import android.app.Application; import android.test.ApplicationTestCase; /** * Testing Fundamentals */ public class ApplicationTest extends ApplicationTestCase { public ApplicationTest() { super(Application.class); } } ================================================ FILE: app/src/main/AndroidManifest.xml ================================================ ================================================ FILE: app/src/main/groovy/zhou/gank/io/App.groovy ================================================ package zhou.gank.io import android.app.Activity import android.app.Application import android.content.ClipData import android.content.ClipboardManager import android.net.Uri import android.os.Environment import android.os.Handler import android.os.Looper import android.support.v7.app.AppCompatActivity import android.widget.Toast import com.bettervectordrawable.VectorDrawableCompat import com.google.gson.Gson import com.google.gson.GsonBuilder import groovy.transform.CompileStatic import zhou.gank.io.comment.Config import zhou.gank.io.database.DatabaseManager import zhou.gank.io.net.NetworkManager import zhou.gank.io.util.Notifier @CompileStatic class App extends Application { public static final String SITE_URL = "http://gank.avosapps.com"; public static final String TYPE_URL = SITE_URL + "/api/data"; public static final String TIME_URL = SITE_URL + "/api/day"; public static final String RANDOM_URL = SITE_URL + "/api/random/data"; public static final File SAVE_PATH = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "gank") static { if (!SAVE_PATH.exists()) { SAVE_PATH.mkdir() } } private static App app; static App getInstance() { return app; } Gson gson; Handler mainHandler int cardLight, cardDark, textLight, textDark ArrayList notifiers public static boolean hasStarted @Override public void onCreate() { super.onCreate(); app = this; DatabaseManager.init(this) cardDark = getColor(R.color.cardview_dark_background) cardLight = getColor(R.color.cardview_light_background) textDark = getColor(R.color.material_grey_50) textLight = getColor(R.color.material_grey_1000) mainHandler = new Handler(Looper.getMainLooper()) gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").create(); NetworkManager.getInstance().init(this, gson) NetworkManager.getInstance().setDefaultHandle { e -> switch (e) { case SocketTimeoutException: return Config.Error.TIME_OUT default: return Config.Error.UNKOWN } } VectorDrawableCompat.enableResourceInterceptionFor(getResources(), R.drawable.ic_favorite_white_48px, R.drawable.ic_info_48px,R.drawable.ic_refresh_48px, R.drawable.ic_dashboard_48px, R.drawable.ic_event_48px, R.drawable.ic_extension_48px, R.drawable.ic_settings_black_48px, R.drawable.ic_menu_white_48px, R.drawable.ic_view_module_48px, R.drawable.ic_cloud_queue_48px, R.drawable.ic_cloud_off_48px, R.drawable.ic_insert_emoticon_48px, R.drawable.ic_bookmark_48px) Config.Configurable.HANDLE_BY_ME = true notifiers = new ArrayList<>() } static void toast(int id) { Toast.makeText(app, id, Toast.LENGTH_SHORT).show(); } static void toast(String msg) { Toast.makeText(app, msg, Toast.LENGTH_SHORT).show(); } static String getStr(int res) { return app.getResources().getString(res); } static int getColor(int res) { return app.getResources().getColor(res) } static File cacheFile() { return app.getCacheDir(); } static void copy(String text) { ClipboardManager myClipboard; myClipboard = (ClipboardManager) app.getSystemService(CLIPBOARD_SERVICE); ClipData myClip; myClip = ClipData.newPlainText("text", text); myClipboard.setPrimaryClip(myClip); } static void copyUri(Uri uri) { ClipboardManager myClipboard; myClipboard = (ClipboardManager) app.getSystemService(CLIPBOARD_SERVICE); ClipData myClip; myClip = ClipData.newUri(app.getContentResolver(), "URI", uri); myClipboard.setPrimaryClip(myClip); } static void setTheme(Activity activity) { String theme = Config.getString(getStr(R.string.key_theme), "light") switch (theme) { case "light": activity.setTheme(R.style.AppTheme) break case "dark": activity.setTheme(R.style.AppThemeDark) break } } static boolean themeIsLight() { String theme = Config.getString(getStr(R.string.key_theme), "light") return theme == "light" } static boolean addNotifier(Notifier notifier) { app.notifiers.add(notifier) } static boolean removeNotifier(Notifier notifier) { app.notifiers.remove(notifier) } static void themeChanged() { app.notifiers.each { def notifier = it as Notifier notifier.notice(Config.Action.RESTART) } } static void setPrimaryColor(AppCompatActivity activity) { } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/MainActivity.groovy ================================================ package zhou.gank.io import android.animation.Animator import android.animation.AnimatorListenerAdapter import android.animation.ObjectAnimator import android.animation.PropertyValuesHolder import android.content.Intent import android.os.Bundle import android.support.annotation.Nullable import android.support.design.widget.FloatingActionButton import android.support.v7.app.AppCompatActivity import android.view.View import android.view.animation.AccelerateDecelerateInterpolator import android.widget.TextView import groovy.transform.CompileStatic import zhou.gank.io.database.DatabaseManager import zhou.gank.io.model.Bookmark import zhou.gank.io.ui.activity.HomeActivity @CompileStatic class MainActivity extends AppCompatActivity { FloatingActionButton fab TextView textView View background @Override protected void onCreate(@Nullable Bundle savedInstanceState) { App.setTheme(this) super.onCreate(savedInstanceState) if (App.hasStarted) { Open() return } else { App.hasStarted = true } setContentView(R.layout.activity_main); fab = findViewById(R.id.fab) as FloatingActionButton textView = findViewById(R.id.textView) as TextView background = findViewById(R.id.background) fab.postDelayed(this.&start, 200) } def void start() { View parent = fab.getParent() as View float scale = Math.sqrt(parent.getHeight() * parent.getHeight() + parent.getWidth() * parent.getWidth()) / fab.getHeight() as float PropertyValuesHolder holderX = PropertyValuesHolder.ofFloat("scaleX", scale) PropertyValuesHolder holderY = PropertyValuesHolder.ofFloat("scaleY", scale) ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(fab as Object, holderX, holderY).setDuration(500) animator.setInterpolator(new AccelerateDecelerateInterpolator()) animator.addListener(new AnimatorListenerAdapter() { @Override void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation) background.setBackgroundColor(MainActivity.this.getResources().getColor(R.color.material_purple_500) as int) fab.setVisibility(View.GONE) textView.setVisibility(View.VISIBLE) } }) parent.getAlpha() PropertyValuesHolder holderA = PropertyValuesHolder.ofFloat("alpha", 0, 1) PropertyValuesHolder holderYm = PropertyValuesHolder.ofFloat("translationY", 0, 300) ObjectAnimator a = ObjectAnimator.ofPropertyValuesHolder(textView as Object, holderA, holderYm).setDuration(700) a.setInterpolator(new AccelerateDecelerateInterpolator()) a.setStartDelay(500) a.addListener(new AnimatorListenerAdapter() { @Override void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation) Open() } }) animator.start() a.start() } public void Open(View view = null) { startActivity(new Intent(this, HomeActivity.class)); finish() } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/comment/Config.groovy ================================================ package zhou.gank.io.comment import android.content.SharedPreferences import android.preference.PreferenceManager import groovy.transform.CompileStatic import zhou.gank.io.App; @CompileStatic class Config { public static class Configurable { public static int DEFAULT_SIZE = 10 public static boolean HANDLE_BY_ME = true public static int MAX_iteration = 7 } public static class Static { public static final String TYPE = "type" public static final String URL = "url" public static final String TITLE = "title" public static final String CONTENT = "content" public static final String IS_RANDOM = "is_random" public static final String IS_IMAGE = "is_image" public static final String IS_MAIN = "is_main" public static final String IS_BOOKMARK = "is_bookmark" public static final String YEAR = "year" public static final String MONTH = "month" public static final String DAY = "day" public static final String URLS = "urls" public static final String POSITION = "position" } public static class Type { public static final String ANDROID = "Android" public static final String IOS = "iOS" public static final String RECOMMEND = "瞎推荐" public static final String RESOURCES = "拓展资源" public static final String WELFARE = "福利" public static final String VIDEO = "休息视频" } public static class Error { public static final String UNKOWN = "未知错误" public static final String TIME_OUT = "网络连接超时" public static final String FORMDATA_ = "数据格式出错" } public static class Action { //打开drawerLayout public static final int OPEN_DRAWER_LAYOUT = 0x111111 public static final int CHANGE_DATE = 0x222222 public static final int FINISH = 0x333333 public static final int RESTART = 0x444444 } public static String getString(String key, String d) { SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(App.getInstance()) preferences.getString(key, d) } public static boolean getBoolean(String key, boolean d) { SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(App.getInstance()) preferences.getBoolean(key, d) } public static int getInt(String key, int d) { SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(App.getInstance()) preferences.getInt(key, d) } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/data/CollectProvider.groovy ================================================ package zhou.gank.io.data import android.support.annotation.Nullable; import groovy.transform.CompileStatic import zhou.gank.io.App import zhou.gank.io.database.DatabaseManager import zhou.gank.io.model.Gank @CompileStatic public class CollectProvider implements DataProvider> { private List ganks @Override void persistence() { } @Override List get() { return ganks } @Override void set(@Nullable List ganks, boolean more) { this.ganks = ganks } @Override void loadByCache(Closure closure) { new Thread({ List gs = DatabaseManager.getInstance().selectToGank() App.getInstance().getMainHandler().post({ closure?.call(gs) }) }).start() } @Override void load(Closure closure, boolean more) { } @Override boolean hasLoad() { return ganks != null } @Override boolean needCache() { return false } @Override boolean clearCache() { return false } @Override String key() { return "bookmark" } @Override void setNoticeable(boolean noticeable) { } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/data/DataManager.groovy ================================================ package zhou.gank.io.data import android.util.Log import groovy.transform.CompileStatic @CompileStatic class DataManager { private static DataManager dataManager public static DataManager getInstance() { if (dataManager == null) { dataManager = new DataManager() } dataManager } private HashMap providers private DataManager() { providers = new HashMap<>() } /** * 添加数据提供器(不进行重复检查) * * @param provider 提供器 */ public void add(DataProvider provider) { if (provider != null) providers.put(provider.key(), provider) } /** * 添加数据提供器 * * @param provider 数据提供器 * @param check 是否进行重复检查 */ public void add(DataProvider provider, boolean check) { if (provider == null) { return } if (check) { if (!providers.containsKey(provider.key())) { providers.put(provider.key(), provider) } } else { providers.put(provider.key(), provider) } } /** * 移除数据提供器 * * @param key key */ public void remove(String key) { providers.remove(key) } /** * 移出数据提供器 * * @param provider 数据提供器 */ public void remove(DataProvider provider) { if (provider != null) { providers.remove(provider.key()) } } /** * 获取数据 按照(内存->本地缓存->网络)的顺序获取 * * @param key key * @param loadCallback 回调 * @param < T > type */ public void get(String key, Closure loadCallback) { try { def provider = providers.get(key) as DataProvider get(provider, loadCallback) } catch (Exception e) { Log.d("get", "DataManager", e) loadCallback?.call() } } /** * 获取数据 * * @param provider 数据提供器 * @param loadCallback 回调 * @param < T > type */ public void get(DataProvider provider, Closure loadCallback) { if (provider.hasLoad()) { loadCallback?.call(provider.get()) } else { provider.loadByCache({t -> if (t != null) { provider.set(t as T, false) if (provider.needCache()) { provider.persistence() } loadCallback?.call(provider.get()) } else { provider.load({tn -> provider.set(tn as T, false); if (provider.needCache()) { provider.persistence() } loadCallback?.call(provider.get()) }, false) } }); } } /** * 加载数据 * * @param key key * @param loadCallback 回调 * @param < T > type */ @SuppressWarnings("unchecked") public void load(String key, Closure loadCallback, boolean more) { try { DataProvider provider = (DataProvider) providers.get(key) load(provider, loadCallback, more) } catch (Exception e) { Log.d("load", "DataManager", e) loadCallback?.call(null) } } /** * 加载数据 * * @param provider 数据提供器 * @param loadCallback 回调 * @param < T > type */ public void load(DataProvider provider, Closure loadCallback, boolean more) { provider.load({t -> provider.set(t as T, more) if (provider.needCache()) { provider.persistence() } loadCallback?.call(provider.get()) }, more); } public void update(DataProvider provider, Closure loadCallback) { load(provider, loadCallback, false) } void update(String key, Closure loadCallback) { load(key, loadCallback, false) } public void more(DataProvider provider, Closure loadCallback) { load(provider, loadCallback, true) } void more(String key, Closure loadCallback) { load(key, loadCallback, true) } /** * 持久化所有数据 */ void persistence() { for (Map.Entry entry : providers.entrySet()) { DataProvider dataProvider = entry.getValue(); if (dataProvider.needCache()) { dataProvider.persistence() } } } void clearAllCache() { for (Map.Entry entry : providers.entrySet()) { DataProvider provider = entry.getValue() provider.clearCache() } } void clearCache(String key) { DataProvider provider = providers.get(key) if (provider != null) { provider.clearCache() } } boolean hasLoad(String key) { DataProvider provider = providers.get(key) provider != null && provider.hasLoad() } boolean exist(String key) { providers.containsKey(key) } public void reset() { providers.clear() } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/data/DataProvider.groovy ================================================ package zhou.gank.io.data import android.support.annotation.NonNull import android.support.annotation.Nullable import groovy.transform.CompileStatic; @CompileStatic interface DataProvider { /** * 数据持久化(应异步进行) */ void persistence(); /** * 获取数据 * * @return 数据 */ @Nullable T get(); /** * 设置数据 * * @param t 数据 */ void set(@Nullable T t, boolean more); /** * 从缓存中加载数据(应异步实现) * * @param loadCallback 回调 */ void loadByCache(Closure closure); /** * 加载数据(必须异步实现) * * @param loadCallback 回调 */ void load(Closure closure, boolean more); /** * 是否已经加载 * * @return boolean */ boolean hasLoad(); /** * 是否需要缓存 * * @return boolean */ boolean needCache(); /** * 清空缓存 */ boolean clearCache(); /** * 获取该加载器的唯一标识 * * @return key */ @NonNull String key(); /** * 设置provide能否发出提醒 * @param noticeable */ void setNoticeable(boolean noticeable) } ================================================ FILE: app/src/main/groovy/zhou/gank/io/data/RandomProvider.groovy ================================================ package zhou.gank.io.data import android.support.annotation.NonNull import android.support.annotation.Nullable import groovy.transform.CompileStatic import zhou.gank.io.App import zhou.gank.io.R import zhou.gank.io.model.Gank import zhou.gank.io.model.Result import zhou.gank.io.net.NetworkManager import zhou.gank.io.util.FileKit import zhou.gank.io.util.HashKit import zhou.gank.io.util.LogKit import zhou.gank.io.util.NetworkKit @CompileStatic class RandomProvider implements DataProvider> { private List ganks private int size private String key, type private File file private boolean noticeable RandomProvider(String type, int size) { this.type = type; this.size = size; key = HashKit.md5("$type-$size-random.cache"); file = new File(App.cacheFile(), key); } @Override void persistence() { if (hasLoad()) { new Thread({ try { FileKit.writeObject(file, ganks) } catch (Exception e) { LogKit.d("persistence", "random", e) } }).start(); } } @Nullable @Override List get() { return ganks; } @Override void set(@Nullable List ganHuos, boolean more) { this.ganks = ganHuos; } @Override void loadByCache(Closure closure) { def gks = null if (file.exists()) { try { gks = FileKit.readObject(file) } catch (Exception e) { LogKit.d("loadByCache", "random", e) } } closure?.call(gks) } @Override void load(Closure closure, boolean more) { if (NetworkManager.getInstance().isNetworkConnected()) { NetworkKit.random(type, size, { result -> def gks = null if (result instanceof Result) { def r = result as Result if (r?.isSuccess()) { gks = r.results } else { if (noticeable) App.toast(R.string.failure_get) } } else { if (noticeable) App.toast(result as String) } closure?.call(gks) }) } else { closure?.call() if (noticeable) App.toast(R.string.error_network) } } @Override boolean hasLoad() { return ganks != null; } @Override boolean needCache() { return false; } @Override boolean clearCache() { return file.exists() && file.delete(); } @NonNull @Override String key() { return key; } @Override void setNoticeable(boolean noticeable) { this.noticeable = noticeable; } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/data/TimeProvider.groovy ================================================ package zhou.gank.io.data import android.support.annotation.NonNull import android.support.annotation.Nullable import groovy.transform.CompileStatic import zhou.gank.io.App import zhou.gank.io.R import zhou.gank.io.model.GankDaily import zhou.gank.io.model.ResultDaily import zhou.gank.io.net.NetworkManager import zhou.gank.io.util.FileKit import zhou.gank.io.util.HashKit import zhou.gank.io.util.LogKit import zhou.gank.io.util.NetworkKit @CompileStatic class TimeProvider implements DataProvider { private GankDaily daily int year, month, day private String key private File file private boolean noticeable private boolean needCache TimeProvider(int year, int month, int day) { this.year = year this.month = month this.day = day key = HashKit.md5("year:$year,month:$month,day:$day-cache") file = new File(App.cacheFile(), key) Calendar now = Calendar.getInstance() int y = now.get(Calendar.YEAR) if (year < y) { needCache = true } else if (year == y) { int m = now.get(Calendar.MONTH) + 1 if (month < m) { needCache = true } else if (month == m) { int d = now.get(Calendar.DAY_OF_MONTH) needCache = day <= d } else { needCache = false } } else { needCache = false } } @Override void persistence() { if (hasLoad()) { new Thread({ try { FileKit.writeObject(file, daily) } catch (Exception e) { LogKit.d("persistence", "time", e) } }).start() } } @Nullable @Override GankDaily get() { return daily } @Override void set(@Nullable GankDaily ganHuos, boolean more) { this.daily = ganHuos } @Override void loadByCache(Closure closure) { def d = null if (file.exists()) { new Thread({ try { d = FileKit.readObject(file) } catch (Exception e) { LogKit.d("loadByCache", "time", e) } App.getInstance().getMainHandler().post({ closure?.call(d) }) }).start() } else { closure?.call(d) } } @Override void load(Closure closure, boolean more) { if (NetworkManager.getInstance().isNetworkConnected()) { NetworkKit.time(year, month, day, { result -> def d = null if (result instanceof ResultDaily) { def r = result as ResultDaily if (r.isSuccess()) { d = r.results } else { if (noticeable) App.toast(R.string.failure_get) } } closure?.call(d) }) } else { closure?.call() if (noticeable) App.toast(R.string.error_network) } } @Override public boolean hasLoad() { return daily != null } @Override public boolean needCache() { return needCache } @Override public boolean clearCache() { return file.exists() && file.delete() } @NonNull @Override public String key() { return key } @Override void setNoticeable(boolean noticeable) { this.noticeable = noticeable } public TimeProvider getNextDay() { Calendar calendar = Calendar.getInstance() calendar.set(Calendar.YEAR, year) calendar.set(Calendar.MONTH, month - 1) calendar.set(Calendar.DAY_OF_MONTH, day) calendar.add(Calendar.DAY_OF_MONTH, 1) return new TimeProvider(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH)) } public TimeProvider getPrevDay() { Calendar calendar = Calendar.getInstance() calendar.set(Calendar.YEAR, year) calendar.set(Calendar.MONTH, month - 1) calendar.set(Calendar.DAY_OF_MONTH, day) calendar.add(Calendar.DAY_OF_MONTH, -1) return new TimeProvider(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH)) } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/data/TypeProvider.groovy ================================================ package zhou.gank.io.data import android.support.annotation.NonNull import android.support.annotation.Nullable import groovy.transform.CompileStatic import zhou.gank.io.App import zhou.gank.io.R import zhou.gank.io.model.Gank import zhou.gank.io.model.Result import zhou.gank.io.net.NetworkManager import zhou.gank.io.util.* @CompileStatic class TypeProvider implements DataProvider> { private List ganks private File file private String type, key private Pageable pageable private boolean noticeable TypeProvider(String type, int size) { this.type = type; key = HashKit.md5("${type}${size}-cache") file = new File(App.cacheFile(), key) pageable = new Pageable(1, size) } @Override void persistence() { if (hasLoad()) { new Thread({ try { FileKit.writeObject(file, new TypeGankEntity(pageable, ganks)) } catch (Exception e) { LogKit.d("persistence", "type", e) } }).start() } } @Nullable @Override List get() { return ganks } @Override void set(@Nullable List ganHuos, boolean more) { if (more && ganHuos != null && hasLoad()) { this.ganks.addAll(ganHuos) } else { this.ganks = ganHuos } } @Override void loadByCache(Closure closure) { def gks = null if (file.exists()) { new Thread({ try { def tge = FileKit.readObject(file) as TypeGankEntity gks = tge.g pageable = tge.p } catch (Exception e) { LogKit.d("loadByCache", "type", e) } }).start() App.getInstance().getMainHandler().post({ closure?.call(gks) }) }else { closure?.call(gks) } } @Override void load(Closure closure, boolean more) { if (NetworkManager.getInstance().isNetworkConnected()) { if (more) { pageable.next() } NetworkKit.type(type, pageable.pageSize, pageable.pageNo, { result -> def gks = null if (result instanceof Result) { def r = result as Result if (r?.isSuccess()) { gks = r.results } else { if (more) { pageable.prev() } if (noticeable) App.toast(R.string.failure_get) } } else { if (noticeable) App.toast(result as String) } closure?.call(gks) }) } else { if (noticeable) App.toast(R.string.error_network) closure?.call() } } @Override boolean hasLoad() { return ganks != null } @Override boolean needCache() { return true } @Override boolean clearCache() { return file.exists() && file.delete() } @NonNull @Override String key() { return key } @Override void setNoticeable(boolean noticeable) { this.noticeable = noticeable } public static class TypeGankEntity implements Serializable { public Pageable p public List g TypeGankEntity(Pageable p, List g) { this.p = p this.g = g } TypeGankEntity() { } } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/database/DatabaseManager.groovy ================================================ package zhou.gank.io.database import android.content.ContentValues import android.content.Context import android.database.Cursor import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteOpenHelper; import groovy.transform.CompileStatic import zhou.gank.io.model.Bookmark import zhou.gank.io.model.Gank @CompileStatic public class DatabaseManager { public static final String TABLE_NAME = "bookmark" public static final String URL = "url" public static final String TITLE = "title" public static final String TIME = "time" private static DatabaseManager databaseManager public static DatabaseManager getInstance() { return databaseManager } public static void init(Context context) { databaseManager = new DatabaseManager(context) } private DatabaseHelper databasehelper private SQLiteDatabase database private DatabaseManager(Context context) { databasehelper = new DatabaseHelper(context) } public void insert(Bookmark bookmark) { start() ContentValues cv = new ContentValues(3) cv.put(URL, bookmark.url) cv.put(TITLE, bookmark.title) cv.put(TIME, bookmark.time.getTime()) database.insert(TABLE_NAME, null, cv) end() } public void delete(String url) { start() database.delete(TABLE_NAME, "url=?", url) end() } public List select() { start() String sql = "select * from $TABLE_NAME order by $TIME desc;" Cursor cursor = database.rawQuery(sql) int count = cursor?.getCount() List bookmarks = new ArrayList<>(count) if (cursor) { cursor.moveToFirst() for (int i = 0; i < count; i++) { String url = cursor.getString(0) String title = cursor.getString(1) long time = cursor.getLong(2) bookmarks.add(new Bookmark(url, title, new Date(time))) cursor.moveToNext() } } end() return bookmarks } public List selectToGank() { start() String sql = "select * from $TABLE_NAME order by $TIME desc;" Cursor cursor = database.rawQuery(sql) int count = cursor?.getCount() List gs = new ArrayList<>(count) if (cursor) { cursor.moveToFirst() for (int i = 0; i < count; i++) { String url = cursor.getString(0) String title = cursor.getString(1) long time = cursor.getLong(2) gs.add(new Gank(title, url, new Date(time))) cursor.moveToNext() } } end() return gs } public boolean isExist(String url) { start() String sql = "select * from $TABLE_NAME where $URL=?" Cursor cursor = database.rawQuery(sql, url) boolean flag = false if (cursor && cursor.getCount() > 0) { flag = true } end() return flag } private void start() { database = databasehelper.getWritableDatabase() } private end() { database?.close() } private class DatabaseHelper extends SQLiteOpenHelper { private static final int VERSION = 1 private static final String DATABASE_NAME = "ganke" DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, VERSION) } @Override void onCreate(SQLiteDatabase sqLiteDatabase) { String sql = "create table $TABLE_NAME (${DatabaseManager.URL} text primary key,${TITLE} text not null,$TIME long not null);" println(sql) sqLiteDatabase.execSQL(sql) } @Override void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { } } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/model/BaseResult.groovy ================================================ package zhou.gank.io.model import groovy.transform.CompileStatic; @CompileStatic abstract class BaseResult implements Serializable { boolean error abstract boolean isSuccess() } ================================================ FILE: app/src/main/groovy/zhou/gank/io/model/Bookmark.groovy ================================================ package zhou.gank.io.model; import groovy.transform.CompileStatic @CompileStatic public class Bookmark implements Serializable { public String url public String title public Date time Bookmark(String url, String title, Date time) { this.url = url this.title = title this.time = time } Bookmark(String url, String title) { this.url = url this.title = title this.time = new Date() } Bookmark() { } @Override public String toString() { return "Bookmark{" + "url='" + url + '\'' + ", title='" + title + '\'' + ", time=" + time + '}'; } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/model/Gank.groovy ================================================ package zhou.gank.io.model import groovy.transform.CompileStatic import groovy.transform.EqualsAndHashCode import groovy.transform.ToString; @ToString @EqualsAndHashCode @CompileStatic class Gank implements Serializable{ public String who public String desc public String type public String url public String objectId public boolean used public Date publishedAt public Date createdAt public Date updatedAt Gank(String who, String desc, String type, String url, String objectId, boolean used, Date publishedAt, Date createdAt, Date updatedAt) { this.who = who this.desc = desc this.type = type this.url = url this.objectId = objectId this.used = used this.publishedAt = publishedAt this.createdAt = createdAt this.updatedAt = updatedAt } Gank(String desc, String url, Date createdAt) { this.desc = desc this.url = url this.createdAt = createdAt } Gank() { } @Override public String toString() { return "Gank{" + "who='" + who + '\'' + ", desc='" + desc + '\'' + ", type='" + type + '\'' + ", url='" + url + '\'' + ", objectId='" + objectId + '\'' + ", used=" + used + ", publishedAt=" + publishedAt + ", createdAt=" + createdAt + ", updatedAt=" + updatedAt + '}'; } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/model/GankDaily.groovy ================================================ package zhou.gank.io.model import groovy.transform.CompileStatic import groovy.transform.ToString; @CompileStatic @ToString(includeNames = true) class GankDaily implements Serializable { public List types; public List> ganks; int size() { return types.size(); } boolean isEmpty() { return types == null ? true : types.isEmpty() || ganks == null ? true : ganks.isEmpty() } String getType(int index) { return types.get(index); } List getGanhuo(int index) { return ganks.get(index); } GankDaily(List types, List> ganks) { this.types = types this.ganks = ganks } GankDaily() { } @Override public String toString() { return "GankDaily{" + "types=" + types + ", ganks=" + ganks + '}'; } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/model/Result.groovy ================================================ package zhou.gank.io.model import groovy.transform.CompileStatic; @CompileStatic class Result extends BaseResult { public List results Result(boolean error, List results) { this.error = error this.results = results } Result() { } @Override public String toString() { return "Result{" + "error=" + error + ", results=" + results + '}'; } @Override boolean isSuccess() { !error } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/model/ResultDaily.groovy ================================================ package zhou.gank.io.model import groovy.transform.CompileStatic import groovy.transform.ToString; @CompileStatic @ToString class ResultDaily extends BaseResult { public GankDaily results; ResultDaily(boolean error, GankDaily results) { this.error = error this.results = results } ResultDaily() { } @Override public String toString() { return "ResultDaily{" + "error=" + error + ", results=" + results + '}'; } @Override boolean isSuccess() { !error } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/net/NetworkManager.groovy ================================================ package zhou.gank.io.net import android.content.Context import android.net.ConnectivityManager import android.net.NetworkInfo import android.os.Handler import android.os.Looper import android.util.Log import com.google.gson.Gson import com.squareup.okhttp.Callback import com.squareup.okhttp.OkHttpClient import com.squareup.okhttp.Request import com.squareup.okhttp.Response import groovy.transform.CompileStatic import zhou.gank.io.model.Result import zhou.gank.io.util.LogKit import java.lang.reflect.Type import java.util.concurrent.TimeUnit //@CompileStatic class NetworkManager { OkHttpClient client Gson gson Handler handler Closure defaultHandle Context context private static NetworkManager networkManager; static NetworkManager getInstance() { return networkManager } static void init(Context context, Gson gson) { networkManager = new NetworkManager(context, gson) } private NetworkManager(Context context, Gson gson) { client = new OkHttpClient() client.setConnectTimeout(5, TimeUnit.SECONDS) client.setReadTimeout(5, TimeUnit.SECONDS) client.setWriteTimeout(5, TimeUnit.SECONDS) this.gson = gson; this.context = context handler = new Handler(Looper.getMainLooper()) } void requestString(Request r, Closure closure) { client.newCall(r).enqueue(new Callback() { @Override void onFailure(Request request, IOException e) { handler.post({ closure(e) }) } @Override void onResponse(Response response) throws IOException { String body = response.body().string(); LogKit.d("requestString", body) handler.post({ closure(body) }) } }) } public void request(Request r, Closure closure, Class aClass) { client.newCall(r).enqueue(new Callback() { @Override void onFailure(Request request, IOException e) { handler.post({ closure(defaultHandle?.call(e)) }) } @Override void onResponse(Response response) throws IOException { String body = response.body().string() Log.d("success", body) def result = gson.fromJson(body, aClass); handler.post({ closure(result) }) } }) Log.d("request", r.urlString()) } public void request(Request r, Closure closure, Type type) { client.newCall(r).enqueue(new Callback() { @Override void onFailure(Request request, IOException e) { handler.post({ closure(defaultHandle?.call(e)) }) } @Override void onResponse(Response response) throws IOException { String body = response.body().string() Log.d("success", body) def result = gson.fromJson(body, type) handler.post({ closure(result) }) } }) } boolean isNetworkConnected() { ConnectivityManager connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo() networkInfo != null && networkInfo.isAvailable() } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/ui/activity/CollectActivity.groovy ================================================ package zhou.gank.io.ui.activity import android.os.Bundle import android.support.annotation.Nullable; import groovy.transform.CompileStatic import zhou.gank.io.App import zhou.gank.io.R import zhou.gank.io.ui.fragment.GankFragment @CompileStatic public class CollectActivity extends ToolbarActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { if (App.themeIsLight()) { setTheme(R.style.AppTheme_Tab) } else { setTheme(R.style.AppTheme_TabDark) } super.onCreate(savedInstanceState) quickFinish() setTitle(R.string.nav_collect) add(GankFragment.newInstance(null, true, false, true)) } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/ui/activity/DailyActivity.groovy ================================================ package zhou.gank.io.ui.activity import android.content.Intent import android.os.Bundle import android.support.annotation.Nullable import android.support.design.widget.CoordinatorLayout import android.support.v4.app.Fragment import android.support.v7.app.AppCompatActivity import com.fourmob.datetimepicker.date.DatePickerDialog import groovy.transform.CompileStatic import zhou.gank.io.App import zhou.gank.io.R import zhou.gank.io.comment.Config import zhou.gank.io.ui.fragment.DailyFragment import zhou.gank.io.util.Notifier import zhou.gank.io.util.TimeKit @CompileStatic public class DailyActivity extends AppCompatActivity implements Notifier { CoordinatorLayout coordinatorLayout; Fragment currFragment int year, month, day @Override protected void onCreate(@Nullable Bundle savedInstanceState) { App.setTheme(this) super.onCreate(savedInstanceState) setContentView(R.layout.activity_compat) def i = getIntent() def times = TimeKit.getTime() year = i.getIntExtra(Config.Static.YEAR, times[0]) month = i.getIntExtra(Config.Static.MONTH, times[1]) day = i.getIntExtra(Config.Static.DAY, times[2]) add(DailyFragment.newInstance(year, month, day)) } def add(Fragment f) { getSupportFragmentManager().beginTransaction().add(R.id.container, f).commit(); this.currFragment = f; } def replace(Fragment f) { if (currFragment == f) { return } coordinatorLayout.removeAllViews() getSupportFragmentManager().beginTransaction().remove(currFragment).add(R.id.container, f).commit() this.currFragment = f } @Override void notice(int noticeId) { switch (noticeId) { case Config.Action.FINISH: finish() break case Config.Action.CHANGE_DATE: DatePickerDialog dialog = DatePickerDialog.newInstance({ datePickerDialog, year, month, day -> Intent i = new Intent(this, DailyActivity.class) i.putExtra(Config.Static.YEAR, year as int) i.putExtra(Config.Static.MONTH, (month as int) + 1) i.putExtra(Config.Static.DAY, day as int) startActivity(i) }, year, month - 1, day) dialog.setVibrate(false) dialog.show(getSupportFragmentManager(), "time") // DatePickerDialog dialog = new DatePickerDialog(this, { picker, year, month, day -> // Intent i = new Intent(this, DailyActivity.class) // i.putExtra(Config.Static.YEAR, year as int) // i.putExtra(Config.Static.MONTH, (month as int) + 1) // i.putExtra(Config.Static.DAY, day as int) // startActivity(i) // }, year, month - 1, day) // dialog.show() // DatePickerDialog dialog = DatePickerDialog.newInstance(this, times[0], times[1] - 1, times[2]) // dialog.vibrate(false) // dialog.show(getFragmentManager(), "date") break } } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/ui/activity/HomeActivity.groovy ================================================ package zhou.gank.io.ui.activity import android.content.Intent import android.os.Bundle import android.support.design.widget.CoordinatorLayout import android.support.design.widget.NavigationView import android.support.v4.app.Fragment import android.support.v4.view.GravityCompat import android.support.v4.widget.DrawerLayout import android.support.v7.app.AppCompatActivity import android.view.MenuItem import com.fourmob.datetimepicker.date.DatePickerDialog import groovy.transform.CompileStatic import zhou.gank.io.App import zhou.gank.io.R import zhou.gank.io.comment.Config import zhou.gank.io.ui.dialog.InfoDialog import zhou.gank.io.ui.fragment.DailyFragment import zhou.gank.io.util.Notifier import zhou.gank.io.util.TextKit import zhou.gank.io.util.TimeKit @CompileStatic class HomeActivity extends AppCompatActivity implements Notifier { static int needRecreate DrawerLayout drawerLayout; NavigationView navigationView; CoordinatorLayout coordinatorLayout; private DailyFragment dailyFragment; private Fragment currFragment; private boolean isRecreate @Override protected void onCreate(Bundle savedInstanceState) { App.setTheme(this) super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); initView(); dailyFragment = DailyFragment.newInstance(-1, -1, -1, true) add(dailyFragment) App.addNotifier(this) } def initView() { drawerLayout = findViewById(R.id.drawer_layout) as DrawerLayout navigationView = findViewById(R.id.nav_view) as NavigationView coordinatorLayout = findViewById(R.id.container) as CoordinatorLayout navigationView.setNavigationItemSelectedListener({ item -> def i = item as MenuItem drawerLayout.closeDrawers() switch (i.getItemId()) { case R.id.nav_daily: replace(dailyFragment) return true case R.id.nav_type: Intent intent = new Intent(this, TabActivity.class) intent.putExtra(Config.Static.IS_RANDOM, false) App.getInstance().getMainHandler().postDelayed({ startActivity(intent) }, 250) return true case R.id.nav_random: Intent intent = new Intent(this, TabActivity.class) intent.putExtra(Config.Static.IS_RANDOM, true) App.getInstance().getMainHandler().postDelayed({ startActivity(intent) }, 250) return true case R.id.nav_collect: Intent intent = new Intent(this, CollectActivity.class) App.getInstance().getMainHandler().postDelayed({ startActivity(intent) }, 250) return true case R.id.nav_info: def info = InfoDialog.newInstance(getString(R.string.nav_info), TextKit.getInfo()) info.show(getSupportFragmentManager(), "info") return true case R.id.nav_setting: def intent1 = new Intent(this, SettingActivity.class) App.getInstance().getMainHandler().postDelayed({ startActivity(intent1) }, 200) return true } return false }) } def add(Fragment f) { getSupportFragmentManager().beginTransaction().add(R.id.container, f).commit(); this.currFragment = f; } def replace(Fragment f) { if (currFragment == f) { return } coordinatorLayout.removeAllViews() getSupportFragmentManager().beginTransaction().remove(currFragment).add(R.id.container, f).commit() this.currFragment = f } def remove() { if (currFragment) { getSupportFragmentManager().beginTransaction().remove(currFragment).commit() coordinatorLayout.removeAllViews() this.currFragment = null } } @Override void notice(int noticeId) { switch (noticeId) { case Config.Action.OPEN_DRAWER_LAYOUT: drawerLayout.openDrawer(GravityCompat.START) break case Config.Action.CHANGE_DATE: def times = TimeKit.getTime() DatePickerDialog dialog = DatePickerDialog.newInstance({ picker, year, month, day -> Intent i = new Intent(this, DailyActivity.class) i.putExtra(Config.Static.YEAR, year as int) i.putExtra(Config.Static.MONTH, (month as int) + 1) i.putExtra(Config.Static.DAY, day as int) startActivity(i) }, times[0], times[1] - 1, times[2]) dialog.setVibrate(false) dialog.show(getSupportFragmentManager(), "date") break case Config.Action.RESTART: needRecreate = 1 break } } @Override protected void onDestroy() { super.onDestroy() App.removeNotifier(this) } @Override protected void onResume() { super.onResume() if (needRecreate == 1) { remove() recreate() needRecreate++ return } if (needRecreate == 2) { App.getInstance().getMainHandler().postDelayed({ dailyFragment.requestData() }, 200) needRecreate = 0 } } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/ui/activity/ImageGalleryActivity.groovy ================================================ package zhou.gank.io.ui.activity import android.content.Intent import android.os.Bundle import android.support.annotation.Nullable import groovy.transform.CompileStatic import zhou.gank.io.comment.Config import zhou.gank.io.ui.fragment.ImageGalleryFragment @CompileStatic public class ImageGalleryActivity extends ToolbarActivity { ArrayList urls @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState) quickFinish() Intent i = getIntent() urls = i.getStringArrayListExtra(Config.Static.URLS) add(ImageGalleryFragment.newInstance(urls, i.getIntExtra(Config.Static.POSITION, 0))) } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/ui/activity/SettingActivity.groovy ================================================ package zhou.gank.io.ui.activity import android.os.Bundle import groovy.transform.CompileStatic import zhou.gank.io.R import zhou.gank.io.ui.fragment.SettingFragment @CompileStatic public class SettingActivity extends ToolbarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState) quickFinish() setTitle(R.string.title_setting) getFragmentManager().beginTransaction().add(R.id.container, new SettingFragment()).commit() } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/ui/activity/TabActivity.groovy ================================================ package zhou.gank.io.ui.activity import android.content.Intent import android.os.Bundle import android.support.annotation.Nullable import android.support.design.widget.TabLayout import android.support.v4.app.Fragment import android.support.v4.app.FragmentPagerAdapter import android.support.v4.view.PagerAdapter import android.support.v4.view.ViewPager import android.support.v7.app.ActionBar import android.support.v7.app.AppCompatActivity import android.support.v7.widget.Toolbar import android.view.MenuItem; import groovy.transform.CompileStatic import zhou.gank.io.App import zhou.gank.io.R import zhou.gank.io.comment.Config import zhou.gank.io.ui.fragment.GankFragment @CompileStatic public class TabActivity extends AppCompatActivity { boolean isRandom Toolbar toolbar TabLayout tabLayout ViewPager viewPager int[] ids = [R.string.nav_android, R.string.nav_ios, R.string.nav_recommend, R.string.nav_resource, R.string.nav_welfare, R.string.nav_video] as int[] Fragment[] fragments @Override protected void onCreate(@Nullable Bundle savedInstanceState) { if (App.themeIsLight()) { setTheme(R.style.AppTheme_Tab) } else { setTheme(R.style.AppTheme_TabDark) } super.onCreate(savedInstanceState) setContentView(R.layout.activity_tab) Intent intent = getIntent() isRandom = intent.getBooleanExtra(Config.Static.IS_RANDOM, false) fragments = new Fragment[6] fragments[0] = GankFragment.newInstance(Config.Type.ANDROID, isRandom) fragments[1] = GankFragment.newInstance(Config.Type.IOS, isRandom) fragments[2] = GankFragment.newInstance(Config.Type.RECOMMEND, isRandom) fragments[3] = GankFragment.newInstance(Config.Type.RESOURCES, isRandom) fragments[4] = GankFragment.newInstance(Config.Type.WELFARE, isRandom, true) fragments[5] = GankFragment.newInstance(Config.Type.VIDEO, isRandom) initView() } void initView() { toolbar = findViewById(R.id.toolbar) as Toolbar setSupportActionBar(toolbar) ActionBar actionBar = getSupportActionBar() actionBar?.setDisplayHomeAsUpEnabled(true) tabLayout = findViewById(R.id.tabs) as TabLayout viewPager = findViewById(R.id.viewpager) as ViewPager ids.each { tabLayout.addTab(tabLayout.newTab().setText(getString(it as Integer))) } PagerAdapter adapter = new FragmentPagerAdapter(getSupportFragmentManager()) { @Override Fragment getItem(int position) { fragments[position] } @Override int getCount() { return fragments.length } @Override CharSequence getPageTitle(int position) { return getString(ids[position]) } } viewPager.setAdapter(adapter) tabLayout.setupWithViewPager(viewPager) tabLayout.setTabsFromPagerAdapter(adapter) if (isRandom) { setTitle(R.string.nav_random) } else { setTitle(R.string.nav_type) } } @Override boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: finish() return true } return super.onOptionsItemSelected(item) } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/ui/activity/ToolbarActivity.groovy ================================================ package zhou.gank.io.ui.activity import android.os.Bundle import android.support.annotation.Nullable import android.support.design.widget.CoordinatorLayout import android.support.v4.app.Fragment import android.support.v7.app.AppCompatActivity import android.support.v7.widget.Toolbar import android.view.KeyEvent import android.view.MenuItem; import groovy.transform.CompileStatic import zhou.gank.io.App import zhou.gank.io.R import zhou.gank.io.ui.fragment.BaseFragment @CompileStatic public class ToolbarActivity extends AppCompatActivity { Toolbar toolbar CoordinatorLayout coordinatorLayout BaseFragment currFragment boolean quick @Override protected void onCreate(@Nullable Bundle savedInstanceState) { App.setTheme(this) super.onCreate(savedInstanceState) setContentView(R.layout.activity_toolbar) toolbar = findViewById(R.id.toolbar) as Toolbar coordinatorLayout = findViewById(R.id.container) as CoordinatorLayout setSupportActionBar(toolbar) } def quickFinish() { def actionBar = getSupportActionBar() actionBar?.setDisplayHomeAsUpEnabled(true) quick = true } def add(BaseFragment f) { getSupportFragmentManager().beginTransaction().add(R.id.container, f).commit(); currFragment = f } def replace(BaseFragment f) { coordinatorLayout.removeAllViews() getSupportFragmentManager().beginTransaction().remove(currFragment).add(R.id.container, f).commit() currFragment = f } @Override boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: if (quick) { finish() return true } break } return super.onOptionsItemSelected(item) } @Override boolean onKeyDown(int keyCode, KeyEvent event) { return currFragment?.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event) } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/ui/activity/WebActivity.groovy ================================================ package zhou.gank.io.ui.activity import android.os.Bundle import android.support.annotation.Nullable import groovy.transform.CompileStatic import zhou.gank.io.R import zhou.gank.io.comment.Config import zhou.gank.io.ui.fragment.WebFragment @CompileStatic public class WebActivity extends ToolbarActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState) quickFinish() def i = getIntent() String title title = i.getStringExtra(Config.Static.TITLE) if (title == null) { title = getString(R.string.app_name) } setTitle(title) add(WebFragment.newInstance(i.getStringExtra(Config.Static.URL), title)) } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/ui/adapter/BaseAdapter.groovy ================================================ package zhou.gank.io.ui.adapter import android.support.v7.widget.RecyclerView import groovy.transform.CompileStatic import zhou.gank.io.model.Gank @CompileStatic public abstract class BaseAdapter extends RecyclerView.Adapter { List ganks Closure clickListener, longClickListener void setGanks(List ganks) { this.ganks = ganks notifyDataSetChanged() } void setClickListener(Closure clickListener) { this.clickListener = clickListener } void setLongClickListener(Closure longClickListener) { this.longClickListener = longClickListener } void removeItem(int position) { } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/ui/adapter/DailyAdapter.groovy ================================================ package zhou.gank.io.ui.adapter import android.support.v7.widget.CardView import android.support.v7.widget.RecyclerView import android.text.method.LinkMovementMethod import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.TextView import groovy.transform.CompileStatic import zhou.gank.io.App import zhou.gank.io.R import zhou.gank.io.model.GankDaily import zhou.gank.io.util.TextKit @CompileStatic class DailyAdapter extends BaseAdapter { private GankDaily daily; @Override Holder onCreateViewHolder(ViewGroup parent, int viewType) { Holder holder = new Holder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_daily, null)); return holder; } @Override void onBindViewHolder(Holder holder, int position) { holder.title.setText(daily.getType(position)); holder.content.setText(TextKit.generate(daily.getGanhuo(position), App.getInstance().getResources().getColor(R.color.material_lightBlue_500))); } @Override int getItemCount() { return daily == null ? 0 : daily.size() } static class Holder extends RecyclerView.ViewHolder { public TextView title, content; public Holder(View itemView) { super(itemView); title = (TextView) itemView.findViewById(R.id.title); content = (TextView) itemView.findViewById(R.id.content); if (itemView instanceof CardView) { def card = itemView as CardView if (App.themeIsLight()) { card.setCardBackgroundColor(App.getInstance().getCardLight()) title.setTextColor(App.getInstance().getTextLight()) } else { card.setCardBackgroundColor(App.getInstance().getCardDark()) title.setTextColor(App.getInstance().getTextDark()) } } content.setMovementMethod(LinkMovementMethod.getInstance()); } } void setDaily(GankDaily daily) { this.daily = daily; notifyDataSetChanged(); } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/ui/adapter/GankAdapter.groovy ================================================ package zhou.gank.io.ui.adapter import android.support.v7.widget.CardView import android.support.v7.widget.RecyclerView import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.TextView import groovy.transform.CompileStatic import zhou.gank.io.App import zhou.gank.io.R import zhou.gank.io.model.Gank import zhou.gank.io.util.TimeKit @CompileStatic public class GankAdapter extends BaseAdapter { List ganks @Override Holder onCreateViewHolder(ViewGroup viewGroup, int i) { def holder = new Holder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_gank, null)) holder.setListener { p -> Gank gank = ganks?.get(p as int) clickListener?.call(gank) } holder.setLongListener { v, p -> Gank gank = ganks?.get(p as int) longClickListener?.call(v, gank, p) } return holder } @Override void onBindViewHolder(Holder holder, int i) { Gank gank = ganks.get(i) holder.title.setText(gank.desc) holder.user.setText(gank.who) holder.time.setText(TimeKit.format(gank.createdAt)) } @Override int getItemCount() { return ganks == null ? 0 : ganks.size() } static class Holder extends RecyclerView.ViewHolder { public TextView title, user, time Closure listener, longListener Holder(View itemView) { super(itemView) title = itemView.findViewById(R.id.title) as TextView user = itemView.findViewById(R.id.user) as TextView time = itemView.findViewById(R.id.time) as TextView if (itemView instanceof CardView) { def card = itemView as CardView if (App.themeIsLight()) { card.setCardBackgroundColor(App.getInstance().getCardLight()) } else { card.setCardBackgroundColor(App.getInstance().getCardDark()) } } itemView.setOnClickListener({ v -> listener?.call(getAdapterPosition()) }) itemView.setOnLongClickListener({ v -> longListener?.call(itemView, getAdapterPosition()) }) } void setListener(Closure listener) { this.listener = listener } } void setGanks(List ganks) { this.ganks = ganks notifyDataSetChanged() } @Override void removeItem(int position) { ganks.remove(position) notifyItemRemoved(position) } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/ui/adapter/ImageAdapter.groovy ================================================ package zhou.gank.io.ui.adapter import android.support.v7.widget.CardView import android.support.v7.widget.RecyclerView import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.ImageView import android.widget.TextView import com.squareup.picasso.Picasso import groovy.transform.CompileStatic import zhou.gank.io.App import zhou.gank.io.R import zhou.gank.io.model.Gank import zhou.gank.io.util.TimeKit @CompileStatic public class ImageAdapter extends BaseAdapter { private List ganks @Override Holder onCreateViewHolder(ViewGroup parent, int viewType) { Holder holder = new Holder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_image, null)) holder.setListener { p -> Gank gank = ganks?.get(p as int) clickListener?.call(gank, p) } return holder } @Override void onBindViewHolder(Holder holder, int position) { Gank gank = ganks.get(position) Picasso.with(holder.icon.getContext()).load(gank.url).into(holder.icon) holder.who.setText(gank.who) holder.time.setText(TimeKit.format(gank.createdAt)) } @Override int getItemCount() { return ganks == null ? 0 : ganks.size() } public static class Holder extends RecyclerView.ViewHolder { ImageView icon TextView who, time Closure listener Holder(View itemView) { super(itemView) icon = itemView.findViewById(R.id.icon) as ImageView who = itemView.findViewById(R.id.who) as TextView time = itemView.findViewById(R.id.time) as TextView if (itemView instanceof CardView) { def card = itemView as CardView if (App.themeIsLight()) { card.setCardBackgroundColor(App.getInstance().getCardLight()) } else { card.setCardBackgroundColor(App.getInstance().getCardDark()) } } itemView.setOnClickListener({ v -> listener?.call(getAdapterPosition()) }) } void setListener(Closure listener) { this.listener = listener } } void setGanks(List ganks) { this.ganks = ganks notifyDataSetChanged() } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/ui/adapter/ImageGalleryAdapter.groovy ================================================ package zhou.gank.io.ui.adapter import android.content.Context import android.support.v4.view.PagerAdapter import android.support.v4.view.ViewPager import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.ImageView import android.widget.TextView import com.squareup.picasso.Callback import com.squareup.picasso.Picasso; import groovy.transform.CompileStatic import zhou.gank.io.R @CompileStatic public class ImageGalleryAdapter extends PagerAdapter { List urls ImageGalleryAdapter(List urls) { this.urls = urls } @Override int getCount() { return urls == null ? 0 : urls.size() } @Override boolean isViewFromObject(View view, Object object) { return view.is(object) } @Override Object instantiateItem(ViewGroup container, int position) { Context context = container.getContext() View v = LayoutInflater.from(context).inflate(R.layout.layout_gallery, null) ImageView imageView = v.findViewById(R.id.image) as ImageView View error = v.findViewById(R.id.error_layout) // View progress = v.findViewById(R.id.progressBar) Closure loadImage = { view -> // progress.setVisibility(View.VISIBLE) error.setVisibility(View.GONE) Picasso.with(context).load(urls[position]).into(imageView, new Callback() { @Override void onSuccess() { // progress.setVisibility(View.GONE) } @Override void onError() { // progress.setVisibility(View.GONE) error.setVisibility(View.VISIBLE) } }) } // error.setOnClickListener(loadImage) loadImage(null) (container as ViewPager).addView(v, 0); return v } @Override void destroyItem(ViewGroup container, int position, Object object) { (container as ViewPager).removeView(object as View); } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/ui/dialog/InfoDialog.groovy ================================================ package zhou.gank.io.ui.dialog import android.app.Dialog import android.os.Bundle import android.support.annotation.Nullable import android.support.v4.app.DialogFragment import android.support.v7.app.AlertController import android.support.v7.app.AlertDialog import android.text.method.LinkMovementMethod import android.view.LayoutInflater import android.widget.TextView; import groovy.transform.CompileStatic import zhou.gank.io.R import zhou.gank.io.comment.Config @CompileStatic public class InfoDialog extends DialogFragment { String title CharSequence content @Override void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState) Bundle b = getArguments() title = b?.getString(Config.Static.TITLE) content = b?.getCharSequence(Config.Static.CONTENT) } @Override Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()) TextView tv = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_text, null) as TextView tv.setText(content) tv.setMovementMethod(LinkMovementMethod.getInstance()) builder.setTitle(title).setView(tv).setPositiveButton(R.string.confirm, null) AlertDialog dialog = builder.create() return dialog } static InfoDialog newInstance(String title, CharSequence content) { InfoDialog dialog = new InfoDialog() Bundle bundle = new Bundle() bundle.putString(Config.Static.TITLE, title) bundle.putCharSequence(Config.Static.CONTENT, content) dialog.setArguments(bundle) return dialog } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/ui/fragment/AdvanceFragment.groovy ================================================ package zhou.gank.io.ui.fragment import android.os.Bundle import android.support.annotation.Nullable import android.support.v4.widget.SwipeRefreshLayout import android.support.v7.widget.RecyclerView import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import groovy.transform.CompileStatic import zhou.gank.io.R @CompileStatic public class AdvanceFragment extends BaseFragment { SwipeRefreshLayout swipeRefreshLayout RecyclerView recyclerView View error @Override View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_recyler_view, container, false) initView(v) return v } @Override protected void initView(View v) { swipeRefreshLayout = v.findViewById(R.id.swipeRefreshLayout) as SwipeRefreshLayout recyclerView = v.findViewById(R.id.recyclerView) as RecyclerView swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_purple, android.R.color.holo_blue_bright, android.R.color.holo_orange_light, android.R.color.holo_red_light); swipeRefreshLayout.setOnRefreshListener(this.&requestRefresh) error = v.findViewById(R.id.error) } protected void onFailure() { swipeRefreshLayout.setRefreshing(false) error.setVisibility(View.VISIBLE) } protected void onSuccess() { swipeRefreshLayout.setRefreshing(false) recyclerView.setVisibility(View.VISIBLE) error.setVisibility(View.GONE) } protected void requestRefresh(View v = null) { swipeRefreshLayout.setRefreshing(true) error.setVisibility(View.GONE) } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/ui/fragment/BaseFragment.groovy ================================================ package zhou.gank.io.ui.fragment import android.app.Activity import android.support.v4.app.Fragment import android.support.v7.app.ActionBar import android.support.v7.app.AppCompatActivity import android.support.v7.widget.Toolbar import android.view.KeyEvent import android.view.View import groovy.transform.CompileStatic import zhou.gank.io.util.Notifier @CompileStatic class BaseFragment extends Fragment { Notifier notifier @Override void onAttach(Activity activity) { super.onAttach(activity) if (activity instanceof Notifier) { notifier = activity as Notifier } } protected void setSupportActionBar(Toolbar toolbar) { Activity activity = getActivity(); if (activity instanceof AppCompatActivity) { AppCompatActivity appCompatActivity = activity as AppCompatActivity; appCompatActivity.setSupportActionBar(toolbar); } } protected ActionBar getSupportActionBar() { Activity activity = getActivity(); if (activity instanceof AppCompatActivity) { AppCompatActivity appCompatActivity = activity as AppCompatActivity; return appCompatActivity.getSupportActionBar(); } return null; } protected void noticeActivity(int noticeId) { notifier?.notice(noticeId) } protected void initView(View v) { } protected void setTitle(String title) { getActivity().setTitle(title) } protected void setTitle(int res) { getActivity().setTitle(res) } boolean onKeyDown(int keyCode, KeyEvent event) { return false } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/ui/fragment/DailyFragment.groovy ================================================ package zhou.gank.io.ui.fragment import android.os.Bundle import android.support.annotation.Nullable import android.support.design.widget.CollapsingToolbarLayout import android.support.design.widget.FloatingActionButton import android.support.v4.app.Fragment import android.support.v4.app.FragmentPagerAdapter import android.support.v4.view.PagerAdapter import android.support.v4.view.ViewPager import android.support.v7.app.ActionBar import android.support.v7.widget.LinearLayoutManager import android.support.v7.widget.RecyclerView import android.support.v7.widget.Toolbar import android.view.* import android.widget.Toast import groovy.transform.CompileStatic import zhou.gank.io.R import zhou.gank.io.comment.Config import zhou.gank.io.data.DataManager import zhou.gank.io.data.TimeProvider import zhou.gank.io.model.Gank import zhou.gank.io.model.GankDaily import zhou.gank.io.ui.adapter.DailyAdapter import zhou.gank.io.util.TimeKit @CompileStatic class DailyFragment extends BaseFragment { public static final int ID_REFRESH = 0x78901 // ImageView icon; RecyclerView recyclerView; Toolbar toolbar; CollapsingToolbarLayout collapsingToolbarLayout; TimeProvider provider DailyAdapter dailyAdapter; ViewPager viewPager FloatingActionButton fab View loading, loadingProgress, empty, error boolean isMain = false int year, month, day int count MenuItem refresh @Override void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); def b = getArguments() List time = TimeKit.getTime() year = time[0] month = time[1] day = time[2] if (b) { year = b.getInt(Config.Static.YEAR, year) month = b.getInt(Config.Static.MONTH, month) day = b.getInt(Config.Static.DAY, day) isMain = b.getBoolean(Config.Static.IS_MAIN, false) } provider = new TimeProvider(year, month, day) provider.setNoticeable(true) } @Nullable @Override View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_daily, container, false); toolbar = view.findViewById(R.id.toolbar) as Toolbar collapsingToolbarLayout = view.findViewById(R.id.collapsing_toolbar) as CollapsingToolbarLayout recyclerView = view.findViewById(R.id.recyclerView) as RecyclerView viewPager = view.findViewById(R.id.viewpager) as ViewPager fab = view.findViewById(R.id.fab) as FloatingActionButton loading = view.findViewById(R.id.loading) loadingProgress = view.findViewById(R.id.progressBar) empty = view.findViewById(R.id.no_data) error = view.findViewById(R.id.error) setSupportActionBar(toolbar); ActionBar ab = getSupportActionBar(); if (ab != null) { if (isMain) ab.setHomeAsUpIndicator(R.drawable.ic_menu_white_48px); ab.setDisplayHomeAsUpEnabled(true); } toolbar.setTitle(R.string.app_name); dailyAdapter = new DailyAdapter(); recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())) recyclerView.setAdapter(dailyAdapter) requestDaily() fab.setOnClickListener({ v -> noticeActivity(Config.Action.CHANGE_DATE) }) error.setOnClickListener({ v -> requestDaily() }) return view; } protected void setUpData(GankDaily daily) { if (daily != null) { if (daily.isEmpty()) { if (isMain) { //为主页的情况 if (count > Config.Configurable.MAX_iteration) { //重复加载次数过多(到达了底端) setTitle(provider.year, provider.month, provider.day) setEmpty() } else { //加载前一天的数据 if (count == 0) { // 重新加载今天的内容 DataManager.getInstance().update(provider, this.&setUpData) count++ } else { count++ provider = provider.getPrevDay() if (TimeKit.future(provider.year, provider.month, provider.day)) { //如如果要加载的数据是今天或以后 DataManager.getInstance().update(provider, this.&setUpData) } else { DataManager.getInstance().get(provider, this.&setUpData) } } } } else { // Empty setTitle(provider.year, provider.month, provider.day) setEmpty() } } else { // Success setTitle(provider.year, provider.month, provider.day) List> ganks = daily.ganks List types = daily.types List welfares = ganks.get(types.indexOf(Config.Type.WELFARE)) int size = welfares?.size() Fragment[] fs = new Fragment[size] size.times { int index = it as int fs[index] = ImagePageFragment.newInstance(welfares.get(index).url) } PagerAdapter adapter = new FragmentPagerAdapter(getFragmentManager()) { @Override Fragment getItem(int i) { fs[i] } @Override int getCount() { return fs.length } } as PagerAdapter viewPager.setAdapter(adapter) dailyAdapter.setDaily(daily) setSuccess() } } else { // Error setTitle(provider.year, provider.month, provider.day) setError() } refresh?.setVisible(true) } @Override void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater) refresh = menu.add(0, ID_REFRESH, 0, R.string.text_refresh) refresh.setIcon(R.drawable.ic_refresh_48px) refresh.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS) refresh.setVisible(false) } @Override boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: if (isMain) { noticeActivity(Config.Action.OPEN_DRAWER_LAYOUT) } else { noticeActivity(Config.Action.FINISH) } return true; case ID_REFRESH: requestUpdate() return true } return super.onOptionsItemSelected(item); } @Override protected void setTitle(String title) { collapsingToolbarLayout.setTitle(title); } def requestDaily() { setTitle(getString(R.string.loading)) setLoading() refresh?.setVisible(false) requestData() } def requestUpdate() { setTitle(getString(R.string.loading)) setLoading() refresh?.setVisible(false) DataManager.getInstance().update(provider, { setUpData(it as GankDaily) Toast.makeText(getActivity(),R.string.success_update,Toast.LENGTH_SHORT).show() }) } def requestData() { DataManager.getInstance().get(provider, this.&setUpData) } def setLoading() { loading.setVisibility(View.VISIBLE) loadingProgress.setVisibility(View.VISIBLE) empty.setVisibility(View.GONE) error.setVisibility(View.GONE) } def setSuccess() { loading.setVisibility(View.INVISIBLE) } def setError() { loading.setVisibility(View.VISIBLE) loadingProgress.setVisibility(View.GONE) empty.setVisibility(View.INVISIBLE) error.setVisibility(View.VISIBLE) } def setEmpty() { loading.setVisibility(View.VISIBLE) loadingProgress.setVisibility(View.GONE) empty.setVisibility(View.VISIBLE) error.setVisibility(View.GONE) } protected void setTitle(int year, int month, int day) { setTitle("${year}${getString(R.string.year)}${month}${getString(R.string.month)}${day}${getString(R.string.day)}") } static DailyFragment newInstance(int year = -1, int month = -1, int day = -1, boolean isMain = false) { DailyFragment fragment = new DailyFragment() Bundle bundle = new Bundle() if (year > 0 && month > 0 && day > 0) { bundle.putInt(Config.Static.YEAR, year) bundle.putInt(Config.Static.MONTH, month) bundle.putInt(Config.Static.DAY, day) } bundle.putBoolean(Config.Static.IS_MAIN, isMain) fragment.setArguments(bundle) return fragment } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/ui/fragment/GankFragment.groovy ================================================ package zhou.gank.io.ui.fragment import android.content.Intent import android.net.Uri import android.os.Bundle import android.support.annotation.Nullable import android.support.v7.widget.LinearLayoutManager import android.support.v7.widget.RecyclerView import android.view.Gravity import android.view.LayoutInflater import android.view.Menu import android.view.MenuItem import android.view.View import android.widget.PopupMenu import android.widget.ProgressBar import android.widget.TextView import android.widget.Toast import groovy.transform.CompileStatic import zhou.gank.io.R import zhou.gank.io.comment.Config import zhou.gank.io.data.CollectProvider import zhou.gank.io.data.DataManager import zhou.gank.io.data.DataProvider import zhou.gank.io.data.RandomProvider import zhou.gank.io.data.TypeProvider import zhou.gank.io.database.DatabaseManager import zhou.gank.io.model.Bookmark import zhou.gank.io.model.Gank import zhou.gank.io.ui.activity.ImageGalleryActivity import zhou.gank.io.ui.activity.WebActivity import zhou.gank.io.ui.adapter.BaseAdapter import zhou.gank.io.ui.adapter.GankAdapter import zhou.gank.io.ui.adapter.ImageAdapter import zhou.gank.io.util.NumKit import zhou.widget.AdvanceAdapter @CompileStatic public class GankFragment extends AdvanceFragment { DataProvider provider String type boolean isRandom, isImage, loadMoreProgress, isBookmark View more LinearLayoutManager manager ProgressBar progressBar TextView moreText private BaseAdapter adapter @Override void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState) def b = getArguments() if (b) { type = b.getString(Config.Static.TYPE) isRandom = b.getBoolean(Config.Static.IS_RANDOM, false) isImage = b.getBoolean(Config.Static.IS_IMAGE, false) isBookmark = b.getBoolean(Config.Static.IS_BOOKMARK, false) } int size = NumKit.getNum(Config.getString(getString(R.string.key_num), "$Config.Configurable.DEFAULT_SIZE"), Config.Configurable.DEFAULT_SIZE) if (isBookmark) { provider = new CollectProvider() } else { if (isRandom) { provider = new RandomProvider(type, size) } else { provider = new TypeProvider(type, size) } } if (isImage) { adapter = new ImageAdapter() } else { adapter = new GankAdapter() } adapter.setClickListener { gank, p = null -> def gs = gank as Gank boolean flag = Config.getBoolean(getString(R.string.key_open), true) if (!flag) { Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(gs.url)) startActivity(intent) } else { Intent intent if (isImage) { intent = new Intent(getActivity(), ImageGalleryActivity.class) ArrayList ganks = provider.get() as ArrayList ArrayList urls = new ArrayList<>(ganks.size()) ganks.each { urls << it.url } intent.putStringArrayListExtra(Config.Static.URLS, urls) intent.putExtra(Config.Static.POSITION, p as int) } else { intent = new Intent(getActivity(), WebActivity.class) intent.putExtra(Config.Static.URL, gs.url) intent.putExtra(Config.Static.TITLE, gs.desc) } startActivity(intent) } } if (isBookmark) { adapter.setLongClickListener { v, gank, p -> def g = gank as Gank def view = v as View PopupMenu popupMenu = new PopupMenu(getActivity(), view, Gravity.END) Menu menu = popupMenu.getMenu() popupMenu.getMenuInflater().inflate(R.menu.menu_pop, menu) popupMenu.setOnMenuItemClickListener({ item -> def i = item as MenuItem switch (i.getItemId()) { case R.id.menu_delete: DatabaseManager.getInstance().delete(g.url) adapter.removeItem(p as int) Toast.makeText(getActivity(), R.string.success_delete, Toast.LENGTH_SHORT).show() return true } return false }) popupMenu.show() return true } } else { adapter.setLongClickListener { v, gank, p -> def g = gank as Gank def view = v as View PopupMenu popupMenu = new PopupMenu(getActivity(), view, Gravity.END) Menu menu = popupMenu.getMenu() if (DatabaseManager.getInstance().isExist(g.url)) { popupMenu.getMenuInflater().inflate(R.menu.menu_pop_remove, menu) } else { popupMenu.getMenuInflater().inflate(R.menu.menu_pop_add, menu) } popupMenu.setOnMenuItemClickListener({ item -> def i = item as MenuItem switch (i.getItemId()) { case R.id.menu_add: DatabaseManager.getInstance().insert(new Bookmark(g.url, g.desc)) Toast.makeText(getActivity(), R.string.success_collect, Toast.LENGTH_SHORT).show() return true case R.id.menu_remove: DatabaseManager.getInstance().delete(g.url) Toast.makeText(getActivity(), R.string.success_uncollect, Toast.LENGTH_SHORT).show() return true } return false }) popupMenu.show() return true } } } public void setUpData(List ganks) { if (ganks != null) { if (ganks.isEmpty()) { onNoMoreData() Toast.makeText(getActivity(), R.string.no_data, Toast.LENGTH_SHORT).show() } else { onSuccess() adapter.setGanks(ganks) showMore() } } else { onLoadFailure() onFailure() } } @Override protected void initView(View v) { super.initView(v) manager = new LinearLayoutManager(getActivity()) recyclerView.setLayoutManager(manager) if (!isRandom) { more = LayoutInflater.from(getActivity()).inflate(R.layout.layout_more, null) moreText = more.findViewById(R.id.textView) as TextView progressBar = more.findViewById(R.id.progressBar) as ProgressBar AdvanceAdapter advanceAdapter = new AdvanceAdapter(adapter) advanceAdapter.addFooter(more) recyclerView.setAdapter(advanceAdapter) recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { if (newState == RecyclerView.SCROLL_STATE_IDLE) { if (more.isShown()) { more() } // if (manager.findLastVisibleItemPosition() == advanceAdapter.getItemCount() - 1) { // more(); // } } } @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { } }); more.setVisibility(View.GONE) more.setClickable(false) more.setOnClickListener(this.&more) } else { recyclerView.setAdapter(adapter) } if (isBookmark) { swipeRefreshLayout.setEnabled(false) } swipeRefreshLayout.setRefreshing(true) provider.setNoticeable(true) DataManager.getInstance().get(provider, this.&setUpData) error.setOnClickListener({ view -> swipeRefreshLayout.setRefreshing(true) error.setVisibility(View.GONE) requestRefresh() }) } @Override void onDestroyView() { super.onDestroyView() provider.setNoticeable(false) } @Override void onHiddenChanged(boolean hidden) { super.onHiddenChanged(hidden) provider.setNoticeable(!hidden) } @Override protected void requestRefresh() { super.requestRefresh() DataManager.getInstance().update(provider, this.&setUpData) } protected void more(View v = null) { if (loadMoreProgress) { return } loadMoreProgress = true onLoading() DataManager.getInstance().more(provider, { ganks -> setUpData(ganks as List) loadMoreProgress = false }) } def onNoMoreData() { moreText?.setText(R.string.more_last) progressBar?.setVisibility(View.INVISIBLE) more?.setClickable(false) } def onLoading() { moreText?.setText(R.string.text_loading) progressBar?.setVisibility(View.VISIBLE) more?.setClickable(false) } def onLoadFailure() { moreText?.setText(R.string.text_loading_failure) progressBar?.setVisibility(View.VISIBLE) more?.setClickable(true) } protected void showMore() { if (manager.getItemCount() > manager.findLastVisibleItemPosition() - manager.findFirstVisibleItemPosition() + 1) more?.setVisibility(View.VISIBLE) } static GankFragment newInstance(String type, boolean isRandom = false, boolean isImage = false, boolean isBookmark = false) { GankFragment fragment = new GankFragment() Bundle bundle = new Bundle() bundle.putString(Config.Static.TYPE, type) bundle.putBoolean(Config.Static.IS_RANDOM, isRandom) bundle.putBoolean(Config.Static.IS_IMAGE, isImage) bundle.putBoolean(Config.Static.IS_BOOKMARK, isBookmark) fragment.setArguments(bundle) fragment } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/ui/fragment/ImageFragment.groovy ================================================ package zhou.gank.io.ui.fragment import android.os.Bundle import android.support.annotation.Nullable import android.support.v7.widget.LinearLayoutManager import android.view.View import android.widget.Toast import groovy.transform.CompileStatic import zhou.gank.io.comment.Config import zhou.gank.io.data.DataProvider import zhou.gank.io.data.RandomProvider import zhou.gank.io.data.TypeProvider import zhou.gank.io.model.Gank import zhou.gank.io.ui.adapter.ImageAdapter @CompileStatic public class ImageFragment extends AdvanceFragment { ImageAdapter adapter LinearLayoutManager manager DataProvider provider boolean isRandom String type View more @Override void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState) def b = getArguments() if (b) { type = b.getString(Config.Static.TYPE) isRandom = b.getBoolean(Config.Static.IS_RANDOM, false) } if (isRandom) { provider = new RandomProvider(type, Config.Configurable.DEFAULT_SIZE) } else { provider = new TypeProvider(type, Config.Configurable.DEFAULT_SIZE) } adapter = new ImageAdapter() } public void setUpData(List ganks) { if (ganks != null) { if (ganks.isEmpty()) { hiddenMore() Toast.makeText(getActivity(), "empty", Toast.LENGTH_SHORT).show() } else { onSuccess() adapter.setGanks(ganks) } } else { onFailure() } } protected void hiddenMore() { more?.setVisibility(View.GONE) } @Override protected void initView(View v) { super.initView(v) manager = new LinearLayoutManager(getActivity()) recyclerView.setLayoutManager(manager) } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/ui/fragment/ImageGalleryFragment.groovy ================================================ package zhou.gank.io.ui.fragment import android.content.Intent import android.graphics.Bitmap import android.graphics.drawable.Drawable import android.net.Uri import android.os.Bundle import android.support.annotation.Nullable import android.support.v4.view.ViewPager import android.view.LayoutInflater import android.view.Menu import android.view.MenuInflater import android.view.MenuItem import android.view.View import android.view.ViewGroup import android.widget.Toast import com.squareup.picasso.Picasso import com.squareup.picasso.Target; import groovy.transform.CompileStatic import zhou.gank.io.App import zhou.gank.io.R import zhou.gank.io.comment.Config import zhou.gank.io.ui.adapter.ImageGalleryAdapter import zhou.gank.io.util.FileKit @CompileStatic public class ImageGalleryFragment extends BaseFragment { public static final int ID_SHARE = 0x23456 public static final int ID_SAVE = 0x34567 List urls int position ViewPager viewPager @Override void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState) setHasOptionsMenu(true) def b = getArguments() urls = b?.getStringArrayList(Config.Static.URLS) position = b?.getInt(Config.Static.POSITION, 0) setTitle("${position + 1}/${urls == null ? 0 : urls.size()}") } @Override View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { viewPager = inflater.inflate(R.layout.fragment_viewpager, container, false) as ViewPager ImageGalleryAdapter adapter = new ImageGalleryAdapter(urls) viewPager.setAdapter(adapter) viewPager.setCurrentItem(position, false) viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { @Override void onPageSelected(int position) { setTitle("${position + 1}/${urls == null ? 0 : urls.size()}") } }) return viewPager } @Override void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater) menu.add(0, ID_SHARE, 0, R.string.share) menu.add(0, ID_SAVE, 0, R.string.save) } @Override boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case ID_SHARE: String url = urls?.get(viewPager.getCurrentItem()) Picasso.with(getActivity()).load(url).into(new Target() { @Override void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { File file = new File(App.getInstance().getExternalCacheDir(), "temp.jpg") FileKit.saveBitmapFile(bitmap, file) Intent intent = new Intent(Intent.ACTION_SEND) intent.setType("image/*") intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)) startActivity(intent) } @Override void onBitmapFailed(Drawable errorDrawable) { Toast.makeText(getActivity(), R.string.error_get_image, Toast.LENGTH_SHORT).show() } @Override void onPrepareLoad(Drawable placeHolderDrawable) { } }) return true case ID_SAVE: String url = urls?.get(viewPager.getCurrentItem()) File file = new File(App.SAVE_PATH, FileKit.getFileRealName(url)) Picasso.with(getActivity()).load(url).into(new Target() { @Override void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { FileKit.saveBitmapFile(bitmap, file) Toast.makeText(getActivity(), "图片保存在:${file.getAbsolutePath()}", Toast.LENGTH_LONG).show() } @Override void onBitmapFailed(Drawable errorDrawable) { Toast.makeText(getActivity(), R.string.error_get_image, Toast.LENGTH_SHORT).show() } @Override void onPrepareLoad(Drawable placeHolderDrawable) { } }) return true } return super.onOptionsItemSelected(item) } static ImageGalleryFragment newInstance(ArrayList urls, int position = 0) { ImageGalleryFragment fragment = new ImageGalleryFragment() Bundle bundle = new Bundle() bundle.putStringArrayList(Config.Static.URLS, urls) bundle.putInt(Config.Static.POSITION, position) fragment.setArguments(bundle) return fragment } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/ui/fragment/ImagePageFragment.groovy ================================================ package zhou.gank.io.ui.fragment import android.content.Intent import android.os.Bundle import android.support.annotation.Nullable import android.support.v4.app.Fragment import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.ImageView import com.squareup.picasso.Picasso; import groovy.transform.CompileStatic import zhou.gank.io.R import zhou.gank.io.comment.Config import zhou.gank.io.ui.activity.ImageGalleryActivity @CompileStatic public class ImagePageFragment extends Fragment { @Override View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { ImageView icon = inflater.inflate(R.layout.fragment_image_page, container, false) as ImageView icon.setClickable(true) def b = getArguments() if (b) { String url = b.getString(Config.Static.URL) Picasso.with(getActivity()).load(url).into(icon) icon.setOnClickListener({ v -> if (icon.getDrawable()) { ArrayList urls = new ArrayList<>(1) urls << url Intent intent = new Intent(getActivity(), ImageGalleryActivity.class) intent.putStringArrayListExtra(Config.Static.URLS, urls) intent.putExtra(Config.Static.POSITION, 0) startActivity(intent) } }) } return icon } static ImagePageFragment newInstance(String url) { ImagePageFragment imagePageFragment = new ImagePageFragment() Bundle bundle = new Bundle() bundle.putString(Config.Static.URL, url) imagePageFragment.setArguments(bundle) return imagePageFragment } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/ui/fragment/SettingFragment.groovy ================================================ package zhou.gank.io.ui.fragment import android.os.Bundle import android.preference.ListPreference import android.preference.Preference import android.preference.PreferenceFragment import android.preference.PreferenceScreen import android.widget.Toast; import groovy.transform.CompileStatic import zhou.gank.io.App import zhou.gank.io.R @CompileStatic public class SettingFragment extends PreferenceFragment { @Override void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState) addPreferencesFromResource(R.xml.setting) ListPreference themes = findPreference(getString(R.string.key_theme)) as ListPreference themes.setOnPreferenceChangeListener({ preference, o -> App.themeChanged() getActivity().recreate() Toast.makeText(getActivity(), "设置成功", Toast.LENGTH_SHORT).show() return true }) } @Override boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { switch (preference.getKey()) { case getString(R.string.key_clear): def g = App.cacheFile().deleteDir() if (g) { App.toast(R.string.success_clear) } } return super.onPreferenceTreeClick(preferenceScreen, preference) } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/ui/fragment/WebFragment.groovy ================================================ package zhou.gank.io.ui.fragment import android.content.Intent import android.net.Uri import android.os.Bundle import android.support.annotation.Nullable import android.support.v4.widget.SwipeRefreshLayout import android.view.KeyEvent import android.view.LayoutInflater import android.view.Menu import android.view.MenuInflater import android.view.MenuItem import android.view.View import android.view.ViewGroup import android.webkit.WebChromeClient import android.webkit.WebSettings import android.webkit.WebView import android.webkit.WebViewClient import android.widget.ProgressBar import android.widget.Toast import groovy.transform.CompileStatic import zhou.gank.io.App import zhou.gank.io.R import zhou.gank.io.comment.Config import zhou.gank.io.database.DatabaseManager import zhou.gank.io.model.Bookmark import zhou.gank.io.util.LogKit @CompileStatic public class WebFragment extends BaseFragment { WebView webView String url, title ProgressBar progressBar SwipeRefreshLayout swipeRefreshLayout MenuItem itemCollect @Override void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState) setHasOptionsMenu(true) } @Override View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_web, container, false) initView(v) return v } @Override protected void initView(View v) { webView = v.findViewById(R.id.web_view) as WebView progressBar = v.findViewById(R.id.progressBar) as ProgressBar swipeRefreshLayout = v.findViewById(R.id.swipeRefreshLayout) as SwipeRefreshLayout swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_purple, android.R.color.holo_blue_bright, android.R.color.holo_orange_light, android.R.color.holo_red_light); webView.getSettings().setJavaScriptEnabled(true) Bundle bundle = getArguments() url = bundle?.getString(Config.Static.URL) title = bundle?.getString(Config.Static.TITLE) webView.loadUrl(url) webView.setWebChromeClient(new WebChromeClient() { @Override public void onProgressChanged(WebView view, int newProgress) { if (newProgress == 100) { // 网页加载完成 progressBar.setVisibility(View.GONE) progressBar.setProgress(0) swipeRefreshLayout.setRefreshing(false) } else { // 加载中 progressBar.setVisibility(View.VISIBLE) progressBar.setProgress(newProgress) } } @Override void onReceivedTitle(WebView view, String title) { super.onReceivedTitle(view, title) getActivity()?.setTitle(title) } }); webView.setWebViewClient(new WebViewClient() { @Override boolean shouldOverrideUrlLoading(WebView view, String url) { webView.loadUrl(url) return true } }) webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); webView.getSettings().setSupportZoom(true) webView.getSettings().setDisplayZoomControls(true) swipeRefreshLayout.setOnRefreshListener({ webView.reload() }) } @Override boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { if (webView.canGoBack()) { webView.goBack()//返回上一页面 return true } else { getActivity().finish() return true } } return super.onKeyDown(keyCode, event); } @Override void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater) inflater.inflate(R.menu.menu_web, menu) itemCollect = menu.findItem(R.id.menu_collect) if (DatabaseManager.getInstance().isExist(url)) { itemCollect.setTitle(R.string.cancel_collect) } else { itemCollect.setTitle(R.string.menu_collect) } } @Override boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_copy: App.copyUri(Uri.parse(url)) App.toast(R.string.success_copy) return true case R.id.menu_open: Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)) startActivity(intent) return true case R.id.menu_collect: collect() return true } return super.onOptionsItemSelected(item) } def collect() { if (DatabaseManager.getInstance().isExist(url)) { try { DatabaseManager.getInstance().delete(url) Toast.makeText(getActivity(), R.string.success_uncollect, Toast.LENGTH_SHORT).show() itemCollect.setTitle(R.string.menu_collect) } catch (Exception e) { LogKit.d("uncollect", "failure", e) Toast.makeText(getActivity(), R.string.failure_uncollect, Toast.LENGTH_SHORT).show() } } else { try { DatabaseManager.getInstance().insert(new Bookmark(url, title == getString(R.string.app_name) ? webView.getTitle() : title)) Toast.makeText(getActivity(), R.string.success_collect, Toast.LENGTH_SHORT).show() itemCollect.setTitle(R.string.cancel_collect) } catch (Exception e) { LogKit.d("collect", "failure", e) Toast.makeText(getActivity(), R.string.failure_collect, Toast.LENGTH_SHORT).show() } } } static WebFragment newInstance(String url, String title = null) { WebFragment webFragment = new WebFragment() Bundle bundle = new Bundle() bundle.putString(Config.Static.URL, url) bundle.putString(Config.Static.TITLE, title) webFragment.setArguments(bundle) return webFragment } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/ui/weiget/SwipeToRefreshLayout.groovy ================================================ package zhou.gank.io.ui.weiget import android.content.Context import android.support.v4.widget.SwipeRefreshLayout import android.util.AttributeSet; import groovy.transform.CompileStatic @CompileStatic public class SwipeToRefreshLayout extends SwipeRefreshLayout{ SwipeToRefreshLayout(Context context) { super(context) } SwipeToRefreshLayout(Context context, AttributeSet attrs) { super(context, attrs) } private boolean mMeasured = false; private boolean mPreMeasureRefreshing = false; @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (!mMeasured) { mMeasured = true setRefreshing(mPreMeasureRefreshing) } } @Override public void setRefreshing(boolean refreshing) { if (mMeasured) { super.setRefreshing(refreshing) } else { mPreMeasureRefreshing = refreshing } } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/ui/weiget/TouchImageView.java ================================================ package zhou.gank.io.ui.weiget; /* * TouchImageView.java * By: Michael Ortiz * Updated By: Patrick Lackemacher * Updated By: Babay88 * Updated By: @ipsilondev * Updated By: hank-cp * Updated By: singpolyma * ------------------- * Extends Android ImageView to include pinch zooming, panning, fling and double tap zoom. */ import android.annotation.TargetApi; import android.content.Context; import android.content.res.Configuration; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.PointF; import android.graphics.RectF; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Build.VERSION; import android.os.Build.VERSION_CODES; import android.os.Bundle; import android.os.Parcelable; import android.util.AttributeSet; import android.util.Log; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.ScaleGestureDetector; import android.view.View; import android.view.animation.AccelerateDecelerateInterpolator; import android.widget.ImageView; import android.widget.OverScroller; import android.widget.Scroller; public class TouchImageView extends ImageView { private static final String DEBUG = "DEBUG"; // // SuperMin and SuperMax multipliers. Determine how much the image can be // zoomed below or above the zoom boundaries, before animating back to the // min/max zoom boundary. // private static final float SUPER_MIN_MULTIPLIER = .75f; private static final float SUPER_MAX_MULTIPLIER = 1.25f; // // Scale of image ranges from minScale to maxScale, where minScale == 1 // when the image is stretched to fit view. // private float normalizedScale; // // Matrix applied to image. MSCALE_X and MSCALE_Y should always be equal. // MTRANS_X and MTRANS_Y are the other values used. prevMatrix is the matrix // saved prior to the screen rotating. // private Matrix matrix, prevMatrix; private enum State {NONE, DRAG, ZOOM, FLING, ANIMATE_ZOOM} private State state; private float minScale; private float maxScale; private float superMinScale; private float superMaxScale; private float[] m; private Context context; private Fling fling; private ScaleType mScaleType; private boolean imageRenderedAtLeastOnce; private boolean onDrawReady; private ZoomVariables delayedZoomVariables; // // Size of view and previous view size (ie before rotation) // private int viewWidth, viewHeight, prevViewWidth, prevViewHeight; // // Size of image when it is stretched to fit view. Before and After rotation. // private float matchViewWidth, matchViewHeight, prevMatchViewWidth, prevMatchViewHeight; private ScaleGestureDetector mScaleDetector; private GestureDetector mGestureDetector; private GestureDetector.OnDoubleTapListener doubleTapListener = null; private OnTouchListener userTouchListener = null; private OnTouchImageViewListener touchImageViewListener = null; public TouchImageView(Context context) { super(context); sharedConstructing(context); } public TouchImageView(Context context, AttributeSet attrs) { super(context, attrs); sharedConstructing(context); } public TouchImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); sharedConstructing(context); } private void sharedConstructing(Context context) { super.setClickable(true); this.context = context; mScaleDetector = new ScaleGestureDetector(context, new ScaleListener()); mGestureDetector = new GestureDetector(context, new GestureListener()); matrix = new Matrix(); prevMatrix = new Matrix(); m = new float[9]; normalizedScale = 1; if (mScaleType == null) { mScaleType = ScaleType.FIT_CENTER; } minScale = 1; maxScale = 3; superMinScale = SUPER_MIN_MULTIPLIER * minScale; superMaxScale = SUPER_MAX_MULTIPLIER * maxScale; setImageMatrix(matrix); setScaleType(ScaleType.MATRIX); setState(State.NONE); onDrawReady = false; super.setOnTouchListener(new PrivateOnTouchListener()); } @Override public void setOnTouchListener(OnTouchListener l) { userTouchListener = l; } public void setOnTouchImageViewListener(OnTouchImageViewListener l) { touchImageViewListener = l; } public void setOnDoubleTapListener(GestureDetector.OnDoubleTapListener l) { doubleTapListener = l; } @Override public void setImageResource(int resId) { super.setImageResource(resId); savePreviousImageValues(); fitImageToView(); } @Override public void setImageBitmap(Bitmap bm) { super.setImageBitmap(bm); savePreviousImageValues(); fitImageToView(); } @Override public void setImageDrawable(Drawable drawable) { super.setImageDrawable(drawable); savePreviousImageValues(); fitImageToView(); } @Override public void setImageURI(Uri uri) { super.setImageURI(uri); savePreviousImageValues(); fitImageToView(); } @Override public void setScaleType(ScaleType type) { if (type == ScaleType.FIT_START || type == ScaleType.FIT_END) { throw new UnsupportedOperationException("TouchImageView does not support FIT_START or FIT_END"); } if (type == ScaleType.MATRIX) { super.setScaleType(ScaleType.MATRIX); } else { mScaleType = type; if (onDrawReady) { // // If the image is already rendered, scaleType has been called programmatically // and the TouchImageView should be updated with the new scaleType. // setZoom(this); } } } @Override public ScaleType getScaleType() { return mScaleType; } /** * Returns false if image is in initial, unzoomed state. False, otherwise. * * @return true if image is zoomed */ public boolean isZoomed() { return normalizedScale != 1; } /** * Return a Rect representing the zoomed image. * * @return rect representing zoomed image */ public RectF getZoomedRect() { if (mScaleType == ScaleType.FIT_XY) { throw new UnsupportedOperationException("getZoomedRect() not supported with FIT_XY"); } PointF topLeft = transformCoordTouchToBitmap(0, 0, true); PointF bottomRight = transformCoordTouchToBitmap(viewWidth, viewHeight, true); float w = getDrawable().getIntrinsicWidth(); float h = getDrawable().getIntrinsicHeight(); return new RectF(topLeft.x / w, topLeft.y / h, bottomRight.x / w, bottomRight.y / h); } /** * Save the current matrix and view dimensions * in the prevMatrix and prevView variables. */ private void savePreviousImageValues() { if (matrix != null && viewHeight != 0 && viewWidth != 0) { matrix.getValues(m); prevMatrix.setValues(m); prevMatchViewHeight = matchViewHeight; prevMatchViewWidth = matchViewWidth; prevViewHeight = viewHeight; prevViewWidth = viewWidth; } } @Override public Parcelable onSaveInstanceState() { Bundle bundle = new Bundle(); bundle.putParcelable("instanceState", super.onSaveInstanceState()); bundle.putFloat("saveScale", normalizedScale); bundle.putFloat("matchViewHeight", matchViewHeight); bundle.putFloat("matchViewWidth", matchViewWidth); bundle.putInt("viewWidth", viewWidth); bundle.putInt("viewHeight", viewHeight); matrix.getValues(m); bundle.putFloatArray("matrix", m); bundle.putBoolean("imageRendered", imageRenderedAtLeastOnce); return bundle; } @Override public void onRestoreInstanceState(Parcelable state) { if (state instanceof Bundle) { Bundle bundle = (Bundle) state; normalizedScale = bundle.getFloat("saveScale"); m = bundle.getFloatArray("matrix"); prevMatrix.setValues(m); prevMatchViewHeight = bundle.getFloat("matchViewHeight"); prevMatchViewWidth = bundle.getFloat("matchViewWidth"); prevViewHeight = bundle.getInt("viewHeight"); prevViewWidth = bundle.getInt("viewWidth"); imageRenderedAtLeastOnce = bundle.getBoolean("imageRendered"); super.onRestoreInstanceState(bundle.getParcelable("instanceState")); return; } super.onRestoreInstanceState(state); } @Override protected void onDraw(Canvas canvas) { onDrawReady = true; imageRenderedAtLeastOnce = true; if (delayedZoomVariables != null) { setZoom(delayedZoomVariables.scale, delayedZoomVariables.focusX, delayedZoomVariables.focusY, delayedZoomVariables.scaleType); delayedZoomVariables = null; } super.onDraw(canvas); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); savePreviousImageValues(); } /** * Get the max zoom multiplier. * * @return max zoom multiplier. */ public float getMaxZoom() { return maxScale; } /** * Set the max zoom multiplier. Default value: 3. * * @param max max zoom multiplier. */ public void setMaxZoom(float max) { maxScale = max; superMaxScale = SUPER_MAX_MULTIPLIER * maxScale; } /** * Get the min zoom multiplier. * * @return min zoom multiplier. */ public float getMinZoom() { return minScale; } /** * Get the current zoom. This is the zoom relative to the initial * scale, not the original resource. * * @return current zoom multiplier. */ public float getCurrentZoom() { return normalizedScale; } /** * Set the min zoom multiplier. Default value: 1. * * @param min min zoom multiplier. */ public void setMinZoom(float min) { minScale = min; superMinScale = SUPER_MIN_MULTIPLIER * minScale; } /** * Reset zoom and translation to initial state. */ public void resetZoom() { normalizedScale = 1; fitImageToView(); } /** * Set zoom to the specified scale. Image will be centered by default. * * @param scale */ public void setZoom(float scale) { setZoom(scale, 0.5f, 0.5f); } /** * Set zoom to the specified scale. Image will be centered around the point * (focusX, focusY). These floats range from 0 to 1 and denote the focus point * as a fraction from the left and top of the view. For example, the top left * corner of the image would be (0, 0). And the bottom right corner would be (1, 1). * * @param scale * @param focusX * @param focusY */ public void setZoom(float scale, float focusX, float focusY) { setZoom(scale, focusX, focusY, mScaleType); } /** * Set zoom to the specified scale. Image will be centered around the point * (focusX, focusY). These floats range from 0 to 1 and denote the focus point * as a fraction from the left and top of the view. For example, the top left * corner of the image would be (0, 0). And the bottom right corner would be (1, 1). * * @param scale * @param focusX * @param focusY * @param scaleType */ public void setZoom(float scale, float focusX, float focusY, ScaleType scaleType) { // // setZoom can be called before the image is on the screen, but at this point, // image and view sizes have not yet been calculated in onMeasure. Thus, we should // delay calling setZoom until the view has been measured. // if (!onDrawReady) { delayedZoomVariables = new ZoomVariables(scale, focusX, focusY, scaleType); return; } if (scaleType != mScaleType) { setScaleType(scaleType); } resetZoom(); scaleImage(scale, viewWidth / 2, viewHeight / 2, true); matrix.getValues(m); m[Matrix.MTRANS_X] = -((focusX * getImageWidth()) - (viewWidth * 0.5f)); m[Matrix.MTRANS_Y] = -((focusY * getImageHeight()) - (viewHeight * 0.5f)); matrix.setValues(m); fixTrans(); setImageMatrix(matrix); } /** * Set zoom parameters equal to another TouchImageView. Including scale, position, * and ScaleType. * * @param TouchImageView */ public void setZoom(TouchImageView img) { PointF center = img.getScrollPosition(); setZoom(img.getCurrentZoom(), center.x, center.y, img.getScaleType()); } /** * Return the point at the center of the zoomed image. The PointF coordinates range * in value between 0 and 1 and the focus point is denoted as a fraction from the left * and top of the view. For example, the top left corner of the image would be (0, 0). * And the bottom right corner would be (1, 1). * * @return PointF representing the scroll position of the zoomed image. */ public PointF getScrollPosition() { Drawable drawable = getDrawable(); if (drawable == null) { return null; } int drawableWidth = drawable.getIntrinsicWidth(); int drawableHeight = drawable.getIntrinsicHeight(); PointF point = transformCoordTouchToBitmap(viewWidth / 2, viewHeight / 2, true); point.x /= drawableWidth; point.y /= drawableHeight; return point; } /** * Set the focus point of the zoomed image. The focus points are denoted as a fraction from the * left and top of the view. The focus points can range in value between 0 and 1. * * @param focusX * @param focusY */ public void setScrollPosition(float focusX, float focusY) { setZoom(normalizedScale, focusX, focusY); } /** * Performs boundary checking and fixes the image matrix if it * is out of bounds. */ private void fixTrans() { matrix.getValues(m); float transX = m[Matrix.MTRANS_X]; float transY = m[Matrix.MTRANS_Y]; float fixTransX = getFixTrans(transX, viewWidth, getImageWidth()); float fixTransY = getFixTrans(transY, viewHeight, getImageHeight()); if (fixTransX != 0 || fixTransY != 0) { matrix.postTranslate(fixTransX, fixTransY); } } /** * When transitioning from zooming from focus to zoom from center (or vice versa) * the image can become unaligned within the view. This is apparent when zooming * quickly. When the content size is less than the view size, the content will often * be centered incorrectly within the view. fixScaleTrans first calls fixTrans() and * then makes sure the image is centered correctly within the view. */ private void fixScaleTrans() { fixTrans(); matrix.getValues(m); if (getImageWidth() < viewWidth) { m[Matrix.MTRANS_X] = (viewWidth - getImageWidth()) / 2; } if (getImageHeight() < viewHeight) { m[Matrix.MTRANS_Y] = (viewHeight - getImageHeight()) / 2; } matrix.setValues(m); } private float getFixTrans(float trans, float viewSize, float contentSize) { float minTrans, maxTrans; if (contentSize <= viewSize) { minTrans = 0; maxTrans = viewSize - contentSize; } else { minTrans = viewSize - contentSize; maxTrans = 0; } if (trans < minTrans) return -trans + minTrans; if (trans > maxTrans) return -trans + maxTrans; return 0; } private float getFixDragTrans(float delta, float viewSize, float contentSize) { if (contentSize <= viewSize) { return 0; } return delta; } private float getImageWidth() { return matchViewWidth * normalizedScale; } private float getImageHeight() { return matchViewHeight * normalizedScale; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { Drawable drawable = getDrawable(); if (drawable == null || drawable.getIntrinsicWidth() == 0 || drawable.getIntrinsicHeight() == 0) { setMeasuredDimension(0, 0); return; } int drawableWidth = drawable.getIntrinsicWidth(); int drawableHeight = drawable.getIntrinsicHeight(); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int widthMode = MeasureSpec.getMode(widthMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); viewWidth = setViewSize(widthMode, widthSize, drawableWidth); viewHeight = setViewSize(heightMode, heightSize, drawableHeight); // // Set view dimensions // setMeasuredDimension(viewWidth, viewHeight); // // Fit content within view // fitImageToView(); } /** * If the normalizedScale is equal to 1, then the image is made to fit the screen. Otherwise, * it is made to fit the screen according to the dimensions of the previous image matrix. This * allows the image to maintain its zoom after rotation. */ private void fitImageToView() { Drawable drawable = getDrawable(); if (drawable == null || drawable.getIntrinsicWidth() == 0 || drawable.getIntrinsicHeight() == 0) { return; } if (matrix == null || prevMatrix == null) { return; } int drawableWidth = drawable.getIntrinsicWidth(); int drawableHeight = drawable.getIntrinsicHeight(); // // Scale image for view // float scaleX = (float) viewWidth / drawableWidth; float scaleY = (float) viewHeight / drawableHeight; switch (mScaleType) { case CENTER: scaleX = scaleY = 1; break; case CENTER_CROP: scaleX = scaleY = Math.max(scaleX, scaleY); break; case CENTER_INSIDE: scaleX = scaleY = Math.min(1, Math.min(scaleX, scaleY)); case FIT_CENTER: scaleX = scaleY = Math.min(scaleX, scaleY); break; case FIT_XY: break; default: // // FIT_START and FIT_END not supported // throw new UnsupportedOperationException("TouchImageView does not support FIT_START or FIT_END"); } // // Center the image // float redundantXSpace = viewWidth - (scaleX * drawableWidth); float redundantYSpace = viewHeight - (scaleY * drawableHeight); matchViewWidth = viewWidth - redundantXSpace; matchViewHeight = viewHeight - redundantYSpace; if (!isZoomed() && !imageRenderedAtLeastOnce) { // // Stretch and center image to fit view // matrix.setScale(scaleX, scaleY); matrix.postTranslate(redundantXSpace / 2, redundantYSpace / 2); normalizedScale = 1; } else { // // These values should never be 0 or we will set viewWidth and viewHeight // to NaN in translateMatrixAfterRotate. To avoid this, call savePreviousImageValues // to set them equal to the current values. // if (prevMatchViewWidth == 0 || prevMatchViewHeight == 0) { savePreviousImageValues(); } prevMatrix.getValues(m); // // Rescale Matrix after rotation // m[Matrix.MSCALE_X] = matchViewWidth / drawableWidth * normalizedScale; m[Matrix.MSCALE_Y] = matchViewHeight / drawableHeight * normalizedScale; // // TransX and TransY from previous matrix // float transX = m[Matrix.MTRANS_X]; float transY = m[Matrix.MTRANS_Y]; // // Width // float prevActualWidth = prevMatchViewWidth * normalizedScale; float actualWidth = getImageWidth(); translateMatrixAfterRotate(Matrix.MTRANS_X, transX, prevActualWidth, actualWidth, prevViewWidth, viewWidth, drawableWidth); // // Height // float prevActualHeight = prevMatchViewHeight * normalizedScale; float actualHeight = getImageHeight(); translateMatrixAfterRotate(Matrix.MTRANS_Y, transY, prevActualHeight, actualHeight, prevViewHeight, viewHeight, drawableHeight); // // Set the matrix to the adjusted scale and translate values. // matrix.setValues(m); } fixTrans(); setImageMatrix(matrix); } /** * Set view dimensions based on layout params * * @param mode * @param size * @param drawableWidth * @return */ private int setViewSize(int mode, int size, int drawableWidth) { int viewSize; switch (mode) { case MeasureSpec.EXACTLY: viewSize = size; break; case MeasureSpec.AT_MOST: viewSize = Math.min(drawableWidth, size); break; case MeasureSpec.UNSPECIFIED: viewSize = drawableWidth; break; default: viewSize = size; break; } return viewSize; } /** * After rotating, the matrix needs to be translated. This function finds the area of image * which was previously centered and adjusts translations so that is again the center, post-rotation. * * @param axis Matrix.MTRANS_X or Matrix.MTRANS_Y * @param trans the value of trans in that axis before the rotation * @param prevImageSize the width/height of the image before the rotation * @param imageSize width/height of the image after rotation * @param prevViewSize width/height of view before rotation * @param viewSize width/height of view after rotation * @param drawableSize width/height of drawable */ private void translateMatrixAfterRotate(int axis, float trans, float prevImageSize, float imageSize, int prevViewSize, int viewSize, int drawableSize) { if (imageSize < viewSize) { // // The width/height of image is less than the view's width/height. Center it. // m[axis] = (viewSize - (drawableSize * m[Matrix.MSCALE_X])) * 0.5f; } else if (trans > 0) { // // The image is larger than the view, but was not before rotation. Center it. // m[axis] = -((imageSize - viewSize) * 0.5f); } else { // // Find the area of the image which was previously centered in the view. Determine its distance // from the left/top side of the view as a fraction of the entire image's width/height. Use that percentage // to calculate the trans in the new view width/height. // float percentage = (Math.abs(trans) + (0.5f * prevViewSize)) / prevImageSize; m[axis] = -((percentage * imageSize) - (viewSize * 0.5f)); } } private void setState(State state) { this.state = state; } public boolean canScrollHorizontallyFroyo(int direction) { return canScrollHorizontally(direction); } @Override public boolean canScrollHorizontally(int direction) { matrix.getValues(m); float x = m[Matrix.MTRANS_X]; if (getImageWidth() < viewWidth) { return false; } else if (x >= -1 && direction < 0) { return false; } else if (Math.abs(x) + viewWidth + 1 >= getImageWidth() && direction > 0) { return false; } return true; } /** * Gesture Listener detects a single click or long click and passes that on * to the view's listener. * * @author Ortiz */ private class GestureListener extends GestureDetector.SimpleOnGestureListener { @Override public boolean onSingleTapConfirmed(MotionEvent e) { if (doubleTapListener != null) { return doubleTapListener.onSingleTapConfirmed(e); } return performClick(); } @Override public void onLongPress(MotionEvent e) { performLongClick(); } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { if (fling != null) { // // If a previous fling is still active, it should be cancelled so that two flings // are not run simultaenously. // fling.cancelFling(); } fling = new Fling((int) velocityX, (int) velocityY); compatPostOnAnimation(fling); return super.onFling(e1, e2, velocityX, velocityY); } @Override public boolean onDoubleTap(MotionEvent e) { boolean consumed = false; if (doubleTapListener != null) { consumed = doubleTapListener.onDoubleTap(e); } if (state == State.NONE) { float targetZoom = (normalizedScale == minScale) ? maxScale : minScale; DoubleTapZoom doubleTap = new DoubleTapZoom(targetZoom, e.getX(), e.getY(), false); compatPostOnAnimation(doubleTap); consumed = true; } return consumed; } @Override public boolean onDoubleTapEvent(MotionEvent e) { return doubleTapListener != null && doubleTapListener.onDoubleTapEvent(e); } } public interface OnTouchImageViewListener { public void onMove(); } /** * Responsible for all touch events. Handles the heavy lifting of drag and also sends * touch events to Scale Detector and Gesture Detector. * * @author Ortiz */ private class PrivateOnTouchListener implements OnTouchListener { // // Remember last point position for dragging // private PointF last = new PointF(); @Override public boolean onTouch(View v, MotionEvent event) { mScaleDetector.onTouchEvent(event); mGestureDetector.onTouchEvent(event); PointF curr = new PointF(event.getX(), event.getY()); if (state == State.NONE || state == State.DRAG || state == State.FLING) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: last.set(curr); if (fling != null) fling.cancelFling(); setState(State.DRAG); break; case MotionEvent.ACTION_MOVE: if (state == State.DRAG) { float deltaX = curr.x - last.x; float deltaY = curr.y - last.y; float fixTransX = getFixDragTrans(deltaX, viewWidth, getImageWidth()); float fixTransY = getFixDragTrans(deltaY, viewHeight, getImageHeight()); matrix.postTranslate(fixTransX, fixTransY); fixTrans(); last.set(curr.x, curr.y); } break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_POINTER_UP: setState(State.NONE); break; } } setImageMatrix(matrix); // // User-defined OnTouchListener // if (userTouchListener != null) { userTouchListener.onTouch(v, event); } // // OnTouchImageViewListener is set: TouchImageView dragged by user. // if (touchImageViewListener != null) { touchImageViewListener.onMove(); } // // indicate event was handled // return true; } } /** * ScaleListener detects user two finger scaling and scales image. * * @author Ortiz */ private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener { @Override public boolean onScaleBegin(ScaleGestureDetector detector) { setState(State.ZOOM); return true; } @Override public boolean onScale(ScaleGestureDetector detector) { scaleImage(detector.getScaleFactor(), detector.getFocusX(), detector.getFocusY(), true); // // OnTouchImageViewListener is set: TouchImageView pinch zoomed by user. // if (touchImageViewListener != null) { touchImageViewListener.onMove(); } return true; } @Override public void onScaleEnd(ScaleGestureDetector detector) { super.onScaleEnd(detector); setState(State.NONE); boolean animateToZoomBoundary = false; float targetZoom = normalizedScale; if (normalizedScale > maxScale) { targetZoom = maxScale; animateToZoomBoundary = true; } else if (normalizedScale < minScale) { targetZoom = minScale; animateToZoomBoundary = true; } if (animateToZoomBoundary) { DoubleTapZoom doubleTap = new DoubleTapZoom(targetZoom, viewWidth / 2, viewHeight / 2, true); compatPostOnAnimation(doubleTap); } } } private void scaleImage(double deltaScale, float focusX, float focusY, boolean stretchImageToSuper) { float lowerScale, upperScale; if (stretchImageToSuper) { lowerScale = superMinScale; upperScale = superMaxScale; } else { lowerScale = minScale; upperScale = maxScale; } float origScale = normalizedScale; normalizedScale *= deltaScale; if (normalizedScale > upperScale) { normalizedScale = upperScale; deltaScale = upperScale / origScale; } else if (normalizedScale < lowerScale) { normalizedScale = lowerScale; deltaScale = lowerScale / origScale; } matrix.postScale((float) deltaScale, (float) deltaScale, focusX, focusY); fixScaleTrans(); } /** * DoubleTapZoom calls a series of runnables which apply * an animated zoom in/out graphic to the image. * * @author Ortiz */ private class DoubleTapZoom implements Runnable { private long startTime; private static final float ZOOM_TIME = 500; private float startZoom, targetZoom; private float bitmapX, bitmapY; private boolean stretchImageToSuper; private AccelerateDecelerateInterpolator interpolator = new AccelerateDecelerateInterpolator(); private PointF startTouch; private PointF endTouch; DoubleTapZoom(float targetZoom, float focusX, float focusY, boolean stretchImageToSuper) { setState(State.ANIMATE_ZOOM); startTime = System.currentTimeMillis(); this.startZoom = normalizedScale; this.targetZoom = targetZoom; this.stretchImageToSuper = stretchImageToSuper; PointF bitmapPoint = transformCoordTouchToBitmap(focusX, focusY, false); this.bitmapX = bitmapPoint.x; this.bitmapY = bitmapPoint.y; // // Used for translating image during scaling // startTouch = transformCoordBitmapToTouch(bitmapX, bitmapY); endTouch = new PointF(viewWidth / 2, viewHeight / 2); } @Override public void run() { float t = interpolate(); double deltaScale = calculateDeltaScale(t); scaleImage(deltaScale, bitmapX, bitmapY, stretchImageToSuper); translateImageToCenterTouchPosition(t); fixScaleTrans(); setImageMatrix(matrix); // // OnTouchImageViewListener is set: double tap runnable updates listener // with every frame. // if (touchImageViewListener != null) { touchImageViewListener.onMove(); } if (t < 1f) { // // We haven't finished zooming // compatPostOnAnimation(this); } else { // // Finished zooming // setState(State.NONE); } } /** * Interpolate between where the image should start and end in order to translate * the image so that the point that is touched is what ends up centered at the end * of the zoom. * * @param t */ private void translateImageToCenterTouchPosition(float t) { float targetX = startTouch.x + t * (endTouch.x - startTouch.x); float targetY = startTouch.y + t * (endTouch.y - startTouch.y); PointF curr = transformCoordBitmapToTouch(bitmapX, bitmapY); matrix.postTranslate(targetX - curr.x, targetY - curr.y); } /** * Use interpolator to get t * * @return */ private float interpolate() { long currTime = System.currentTimeMillis(); float elapsed = (currTime - startTime) / ZOOM_TIME; elapsed = Math.min(1f, elapsed); return interpolator.getInterpolation(elapsed); } /** * Interpolate the current targeted zoom and get the delta * from the current zoom. * * @param t * @return */ private double calculateDeltaScale(float t) { double zoom = startZoom + t * (targetZoom - startZoom); return zoom / normalizedScale; } } /** * This function will transform the coordinates in the touch event to the coordinate * system of the drawable that the imageview contain * * @param x x-coordinate of touch event * @param y y-coordinate of touch event * @param clipToBitmap Touch event may occur within view, but outside image content. True, to clip return value * to the bounds of the bitmap size. * @return Coordinates of the point touched, in the coordinate system of the original drawable. */ private PointF transformCoordTouchToBitmap(float x, float y, boolean clipToBitmap) { matrix.getValues(m); float origW = getDrawable().getIntrinsicWidth(); float origH = getDrawable().getIntrinsicHeight(); float transX = m[Matrix.MTRANS_X]; float transY = m[Matrix.MTRANS_Y]; float finalX = ((x - transX) * origW) / getImageWidth(); float finalY = ((y - transY) * origH) / getImageHeight(); if (clipToBitmap) { finalX = Math.min(Math.max(finalX, 0), origW); finalY = Math.min(Math.max(finalY, 0), origH); } return new PointF(finalX, finalY); } /** * Inverse of transformCoordTouchToBitmap. This function will transform the coordinates in the * drawable's coordinate system to the view's coordinate system. * * @param bx x-coordinate in original bitmap coordinate system * @param by y-coordinate in original bitmap coordinate system * @return Coordinates of the point in the view's coordinate system. */ private PointF transformCoordBitmapToTouch(float bx, float by) { matrix.getValues(m); float origW = getDrawable().getIntrinsicWidth(); float origH = getDrawable().getIntrinsicHeight(); float px = bx / origW; float py = by / origH; float finalX = m[Matrix.MTRANS_X] + getImageWidth() * px; float finalY = m[Matrix.MTRANS_Y] + getImageHeight() * py; return new PointF(finalX, finalY); } /** * Fling launches sequential runnables which apply * the fling graphic to the image. The values for the translation * are interpolated by the Scroller. * * @author Ortiz */ private class Fling implements Runnable { CompatScroller scroller; int currX, currY; Fling(int velocityX, int velocityY) { setState(State.FLING); scroller = new CompatScroller(context); matrix.getValues(m); int startX = (int) m[Matrix.MTRANS_X]; int startY = (int) m[Matrix.MTRANS_Y]; int minX, maxX, minY, maxY; if (getImageWidth() > viewWidth) { minX = viewWidth - (int) getImageWidth(); maxX = 0; } else { minX = maxX = startX; } if (getImageHeight() > viewHeight) { minY = viewHeight - (int) getImageHeight(); maxY = 0; } else { minY = maxY = startY; } scroller.fling(startX, startY, (int) velocityX, (int) velocityY, minX, maxX, minY, maxY); currX = startX; currY = startY; } public void cancelFling() { if (scroller != null) { setState(State.NONE); scroller.forceFinished(true); } } @Override public void run() { // // OnTouchImageViewListener is set: TouchImageView listener has been flung by user. // Listener runnable updated with each frame of fling animation. // if (touchImageViewListener != null) { touchImageViewListener.onMove(); } if (scroller.isFinished()) { scroller = null; return; } if (scroller.computeScrollOffset()) { int newX = scroller.getCurrX(); int newY = scroller.getCurrY(); int transX = newX - currX; int transY = newY - currY; currX = newX; currY = newY; matrix.postTranslate(transX, transY); fixTrans(); setImageMatrix(matrix); compatPostOnAnimation(this); } } } @TargetApi(VERSION_CODES.GINGERBREAD) private class CompatScroller { Scroller scroller; OverScroller overScroller; boolean isPreGingerbread; public CompatScroller(Context context) { if (VERSION.SDK_INT < VERSION_CODES.GINGERBREAD) { isPreGingerbread = true; scroller = new Scroller(context); } else { isPreGingerbread = false; overScroller = new OverScroller(context); } } public void fling(int startX, int startY, int velocityX, int velocityY, int minX, int maxX, int minY, int maxY) { if (isPreGingerbread) { scroller.fling(startX, startY, velocityX, velocityY, minX, maxX, minY, maxY); } else { overScroller.fling(startX, startY, velocityX, velocityY, minX, maxX, minY, maxY); } } public void forceFinished(boolean finished) { if (isPreGingerbread) { scroller.forceFinished(finished); } else { overScroller.forceFinished(finished); } } public boolean isFinished() { if (isPreGingerbread) { return scroller.isFinished(); } else { return overScroller.isFinished(); } } public boolean computeScrollOffset() { if (isPreGingerbread) { return scroller.computeScrollOffset(); } else { overScroller.computeScrollOffset(); return overScroller.computeScrollOffset(); } } public int getCurrX() { if (isPreGingerbread) { return scroller.getCurrX(); } else { return overScroller.getCurrX(); } } public int getCurrY() { if (isPreGingerbread) { return scroller.getCurrY(); } else { return overScroller.getCurrY(); } } } @TargetApi(VERSION_CODES.JELLY_BEAN) private void compatPostOnAnimation(Runnable runnable) { if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) { postOnAnimation(runnable); } else { postDelayed(runnable, 1000 / 60); } } private class ZoomVariables { public float scale; public float focusX; public float focusY; public ScaleType scaleType; public ZoomVariables(float scale, float focusX, float focusY, ScaleType scaleType) { this.scale = scale; this.focusX = focusX; this.focusY = focusY; this.scaleType = scaleType; } } private void printMatrixInfo() { float[] n = new float[9]; matrix.getValues(n); Log.d(DEBUG, "Scale: " + n[Matrix.MSCALE_X] + " TransX: " + n[Matrix.MTRANS_X] + " TransY: " + n[Matrix.MTRANS_Y]); } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/ui/weiget/URLSpanNoUnderline.groovy ================================================ package zhou.gank.io.ui.weiget import android.content.Intent import android.net.Uri import android.os.Parcel import android.support.annotation.NonNull import android.text.SpannableStringBuilder import android.text.TextPaint import android.text.style.URLSpan import android.view.View import android.widget.TextView import groovy.transform.CompileStatic import zhou.gank.io.App import zhou.gank.io.R import zhou.gank.io.comment.Config import zhou.gank.io.ui.activity.WebActivity import zhou.gank.io.util.LogKit; @CompileStatic class URLSpanNoUnderline extends URLSpan { URLSpanNoUnderline(String url) { super(url) } URLSpanNoUnderline(Parcel src) { super(src) } @Override public void updateDrawState(@NonNull TextPaint ds) { super.updateDrawState(ds); ds.setUnderlineText(false); } @Override void onClick(View widget) { def open = Config.getBoolean(widget.getResources().getString(R.string.key_open), Config.Configurable.HANDLE_BY_ME); if (open) { Intent intent = new Intent(widget.getContext(), WebActivity.class) intent.putExtra(Config.Static.URL, getURL()) widget.getContext().startActivity(intent) } else { super.onClick(widget) } } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/util/FileKit.java ================================================ package zhou.gank.io.util; import android.graphics.Bitmap; import android.util.Log; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.OutputStreamWriter; import java.text.DecimalFormat; /** * Created by 州 on 2015/7/4 0004. * 文件操作相关工具类 */ public class FileKit { public static DecimalFormat decimalFormat = new DecimalFormat(".00"); /** * 判断文件是否存在 * * @param path 文件的全路径 * @return 文件是否存在 */ @SuppressWarnings("unused") public static boolean isFileExists(String path) { File file = new File(path); return file.exists(); } @SuppressWarnings("unused") public static boolean isFileExists(File parent, String name) { File file = new File(parent, name); return file.exists(); } /** * 获取文件的后缀名 * * @param path 文件全路径 * @return 后缀名 */ @SuppressWarnings("unused") public static String getFileExtension(String path) { return path == null ? null : path.substring(path.lastIndexOf(".") + 1); } /** * 去掉路径的后缀名 * * @param path 文件全路径 * @return 去后缀名后的文件名 */ @SuppressWarnings("unused") public static String getPathWithoutExtension(String path) { return path == null ? null : path.substring(0, path.lastIndexOf(".")); } /** * 将对象写入指定路径的文件中 * * @param path 文件路径 * @param obj 需要被写入的对象 */ @SuppressWarnings("unused") public static void writeObject(String path, Object obj) { File file = new File(path); writeObject(file, obj); } /** * 写入对象到文件中 * * @param file 文件对象 * @param obj 需要写入文件的对象 */ @SuppressWarnings("unused") public static void writeObject(File file, Object obj) { if (null == file || obj == null) { return; } FileOutputStream fileOutputStream = null; ObjectOutputStream objectOutputStream = null; try { fileOutputStream = new FileOutputStream(file); objectOutputStream = new ObjectOutputStream(fileOutputStream); objectOutputStream.writeObject(obj); objectOutputStream.flush(); } catch (IOException e) { Log.d("writeObject", e.getMessage()); } finally { try { if (objectOutputStream != null) { objectOutputStream.close(); } if (fileOutputStream != null) { fileOutputStream.close(); } } catch (IOException e) { Log.d("writeObject", e.getMessage()); } } } /** * 从指定路径的文件中读取对象 * * @param path 文件路径 * @return 读取到的对象 */ @SuppressWarnings("unused") public static Object readObject(String path) { File file = new File(path); return readObject(file); } /** * 从文件中读取对象 * * @param file 文件对象 * @return 读取到的对象 */ public static Object readObject(File file) { Object obj = null; if (file != null && file.exists()) { FileInputStream fileInputStream = null; ObjectInputStream objectInputStream = null; try { fileInputStream = new FileInputStream(file); objectInputStream = new ObjectInputStream(fileInputStream); obj = objectInputStream.readObject(); } catch (IOException | ClassNotFoundException e) { Log.d("readObject", e.getMessage()); } finally { try { if (objectInputStream != null) { objectInputStream.close(); } if (fileInputStream != null) { fileInputStream.close(); } } catch (IOException e) { Log.d("readObject", e.getMessage()); } } } return obj; } public static void writeString(File file, String content) { FileOutputStream fileOutputStream = null; OutputStreamWriter outputStreamWriter = null; BufferedWriter bufferedWriter = null; try { fileOutputStream = new FileOutputStream(file); outputStreamWriter = new OutputStreamWriter(fileOutputStream); bufferedWriter = new BufferedWriter(outputStreamWriter); bufferedWriter.write(content); } catch (IOException e) { Log.d("writeString", "error", e); } finally { try { if (bufferedWriter != null) { bufferedWriter.close(); } if (outputStreamWriter != null) { outputStreamWriter.close(); } if (fileOutputStream != null) { fileOutputStream.close(); } } catch (IOException e) { Log.d("writeString", "error final", e); } } } public static String readString(File file) { FileInputStream fileInputStream = null; InputStreamReader inputStreamReader = null; BufferedReader bufferedReader = null; String content = null; try { fileInputStream = new FileInputStream(file); inputStreamReader = new InputStreamReader(fileInputStream); bufferedReader = new BufferedReader(inputStreamReader); String line; StringBuilder sb = new StringBuilder(); while ((line = bufferedReader.readLine()) != null) { sb.append(line).append("\n"); } sb.deleteCharAt(sb.length() - 1); content = sb.toString(); } catch (FileNotFoundException e) { Log.d("readString", "error", e); } catch (IOException e) { e.printStackTrace(); } finally { try { if (bufferedReader != null) { bufferedReader.close(); } if (inputStreamReader != null) { inputStreamReader.close(); } if (fileInputStream != null) { fileInputStream.close(); } } catch (IOException e) { Log.d("readString", "error final", e); } } return content; } /** * 格式化文件大小以字符串输出 * * @param size 大小 * @return B、KB、MB类型的字符串 */ @SuppressWarnings("unused") public static String formatSize(int size) { if (size < 1024 * 0.6) { return size + "B"; } else if (size < 1024 * 1024 * 0.6) { return decimalFormat.format((float) size / 1024) + "KB"; } else { return decimalFormat.format((float) size / (1024 * 1024)) + "MB"; } } public static void saveBitmapFile(Bitmap bitmap, File file) { FileOutputStream fileOutputStream = null; BufferedOutputStream bufferedOutputStream = null; try { fileOutputStream = new FileOutputStream(file); bufferedOutputStream = new BufferedOutputStream(fileOutputStream); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bufferedOutputStream); bufferedOutputStream.flush(); bufferedOutputStream.close(); } catch (IOException e) { LogKit.d("saveBitmapFile", "error", e); } finally { try { if (bufferedOutputStream != null) { bufferedOutputStream.close(); } if (fileOutputStream != null) { fileOutputStream.close(); } } catch (IOException e) { LogKit.d("saveBitmapFile", "error", e); } } } public static String getFileRealName(String path) { return path.substring(path.lastIndexOf("/") + 1); } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/util/HashKit.java ================================================ package zhou.gank.io.util; import java.security.MessageDigest; /** * Created by zzhoujay on 2015/7/31 0031. * 加密相关工具类 */ public class HashKit { private static java.security.SecureRandom random = new java.security.SecureRandom(); public static String md5(String srcStr){ return hash("MD5", srcStr); } public static String sha1(String srcStr){ return hash("SHA-1", srcStr); } public static String sha256(String srcStr){ return hash("SHA-256", srcStr); } public static String sha384(String srcStr){ return hash("SHA-384", srcStr); } public static String sha512(String srcStr){ return hash("SHA-512", srcStr); } public static String hash(String algorithm, String srcStr) { try { StringBuilder result = new StringBuilder(); MessageDigest md = MessageDigest.getInstance(algorithm); byte[] bytes = md.digest(srcStr.getBytes("utf-8")); for (byte b : bytes) { String hex = Integer.toHexString(b&0xFF); if (hex.length() == 1) result.append("0"); result.append(hex); } return result.toString(); } catch (Exception e) { throw new RuntimeException(e); } } private static String toHex(byte[] bytes) { StringBuilder result = new StringBuilder(); for (byte b : bytes) { String hex = Integer.toHexString(b&0xFF); if (hex.length() == 1) result.append("0"); result.append(hex); } return result.toString(); } /** * md5 128bit 16bytes * sha1 160bit 20bytes * sha256 256bit 32bytes * sha384 384bit 48bites * sha512 512bit 64bites */ public static String generateSalt(int numberOfBytes) { byte[] salt = new byte[numberOfBytes]; random.nextBytes(salt); return toHex(salt); } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/util/JsonKit.groovy ================================================ package zhou.gank.io.util import com.google.gson.Gson import com.google.gson.JsonArray import com.google.gson.JsonElement import com.google.gson.JsonObject import com.google.gson.JsonParser import com.google.gson.reflect.TypeToken import groovy.transform.CompileStatic import zhou.gank.io.model.Gank import zhou.gank.io.model.GankDaily import zhou.gank.io.model.ResultDaily @CompileStatic class JsonKit { public static ResultDaily generate(String json, Gson gson) { JsonParser parser = new JsonParser(); JsonElement element = parser.parse(json); JsonObject root = element.getAsJsonObject(); JsonArray category = root.getAsJsonArray("category"); Iterator iterator = category.iterator(); JsonObject results = root.getAsJsonObject("results"); boolean error = root.get("error").getAsBoolean(); ArrayList types = new ArrayList<>(category.size()); ArrayList> ghs = new ArrayList<>(category.size()); while (iterator.hasNext()) { JsonElement type = iterator.next(); String t = type.getAsString(); types.add(t); String e = results.get(t).toString(); ghs.add(gson.fromJson(e, new TypeToken>() { }.getType()) as List); } ghs.sort { a, b -> def aa = a as List def bb = b as List return aa.get(0).type.compareTo(bb.get(0).type) } types.sort() return new ResultDaily(error, new GankDaily(types, ghs)); } public static final String test = "{\"error\":false,\"results\":{\"iOS\":[{\"who\":\"CallMeWhy\",\"publishedAt\":\"2015-08-07T03:57:48.070Z\",\"desc\":\"LLVM 简介\",\"type\":\"iOS\",\"url\":\"http://adriansampson.net/blog/llvm.html\",\"used\":true,\"objectId\":\"55c40ac360b2c9d32a67ca25\",\"createdAt\":\"2015-08-07T01:32:51.588Z\",\"updatedAt\":\"2015-08-15T03:15:54.384Z\"},{\"who\":\"鲍永章\",\"publishedAt\":\"2015-08-07T03:57:47.242Z\",\"desc\":\"基于TextKit的UILabel,支持超链接和自定义表达式。\",\"type\":\"iOS\",\"url\":\"https://github.com/molon/MLLabel\",\"used\":true,\"objectId\":\"55c372fe60b2f809e41eb388\",\"createdAt\":\"2015-08-06T14:45:18.733Z\",\"updatedAt\":\"2015-08-15T03:15:54.536Z\"},{\"who\":\"CallMeWhy\",\"publishedAt\":\"2015-08-07T03:57:48.083Z\",\"desc\":\"Swift 和 C 函数\",\"type\":\"iOS\",\"url\":\"http://chris.eidhof.nl/posts/swift-c-interop.html\",\"used\":true,\"objectId\":\"55c40aea00b025867b19c9af\",\"createdAt\":\"2015-08-07T01:33:30.871Z\",\"updatedAt\":\"2015-08-15T03:15:54.408Z\"},{\"who\":\"CallMeWhy\",\"publishedAt\":\"2015-08-07T03:57:48.174Z\",\"desc\":\"Arrays Linked Lists 和性能比较\",\"type\":\"iOS\",\"url\":\"http://airspeedvelocity.net/2015/08/03/arrays-linked-lists-and-performance/\",\"used\":true,\"objectId\":\"55c40b0800b0fac2c2809acc\",\"createdAt\":\"2015-08-07T01:34:00.984Z\",\"updatedAt\":\"2015-08-15T03:15:55.105Z\"}],\"Android\":[{\"who\":\"mthli\",\"publishedAt\":\"2015-08-07T03:57:48.045Z\",\"desc\":\"类似Link Bubble的悬浮式操作设计\",\"type\":\"Android\",\"url\":\"https://github.com/recruit-lifestyle/FloatingView\",\"used\":true,\"objectId\":\"55c309a800b00045333db517\",\"createdAt\":\"2015-08-06T07:15:52.065Z\",\"updatedAt\":\"2015-08-15T03:15:55.098Z\"},{\"who\":\"lxxself\",\"publishedAt\":\"2015-08-07T03:57:47.317Z\",\"desc\":\"Android开发中,有哪些让你觉得相见恨晚的方法、类或接口?\",\"type\":\"Android\",\"url\":\"http://www.zhihu.com/question/33636939\",\"used\":true,\"objectId\":\"55c40ad340ac7d0a9507b324\",\"createdAt\":\"2015-08-07T01:33:07.815Z\",\"updatedAt\":\"2015-08-15T03:15:54.407Z\"},{\"who\":\"鲍永章\",\"publishedAt\":\"2015-08-07T03:57:48.076Z\",\"desc\":\"使用注解来处理Activity的状态恢复\",\"type\":\"Android\",\"url\":\"https://github.com/tom91136/Akatsuki\",\"used\":true,\"objectId\":\"55c3769660b2750766971ce6\",\"createdAt\":\"2015-08-06T15:00:38.350Z\",\"updatedAt\":\"2015-08-15T03:15:54.866Z\"},{\"who\":\"有时放纵\",\"publishedAt\":\"2015-08-07T03:57:48.142Z\",\"desc\":\"Android Lollipop联系人之PinnedListView简单使用\",\"type\":\"Android\",\"url\":\"https://git.oschina.net/way/PinnedHeaderListView\",\"used\":true,\"objectId\":\"55c415c060b2d140ca882eac\",\"createdAt\":\"2015-08-07T02:19:44.342Z\",\"updatedAt\":\"2015-08-15T03:15:55.020Z\"},{\"who\":\"鲍永章\",\"publishedAt\":\"2015-08-07T03:57:48.073Z\",\"desc\":\"图片可以自动滚动的ImageView,可以实现视差效果。\",\"type\":\"Android\",\"url\":\"https://github.com/Q42/AndroidScrollingImageView\",\"used\":true,\"objectId\":\"55c3761400b00045334480d2\",\"createdAt\":\"2015-08-06T14:58:28.171Z\",\"updatedAt\":\"2015-08-15T03:15:55.123Z\"}],\"瞎推荐\":[{\"who\":\"lxxself\",\"publishedAt\":\"2015-08-07T03:57:48.084Z\",\"desc\":\"程序员的电台FmM,这个页面chrome插件有问题啊哭,我写了回删除不了啊\",\"type\":\"瞎推荐\",\"url\":\"https://cmd.fm/\",\"used\":true,\"objectId\":\"55c40f5e00b00045334934b4\",\"createdAt\":\"2015-08-07T01:52:30.267Z\",\"updatedAt\":\"2015-08-15T03:15:54.383Z\"}],\"拓展资源\":[{\"who\":\"lxxself\",\"publishedAt\":\"2015-08-07T03:57:48.081Z\",\"desc\":\"Display GitHub code in tree format\",\"type\":\"拓展资源\",\"url\":\"https://github.com/buunguyen/octotree\",\"used\":true,\"objectId\":\"55c40b8600b08484a7f3a032\",\"createdAt\":\"2015-08-07T01:36:06.932Z\",\"updatedAt\":\"2015-08-15T03:15:54.382Z\"}],\"福利\":[{\"who\":\"张涵宇\",\"publishedAt\":\"2015-08-07T03:57:47.310Z\",\"desc\":\"8.7——(1)\",\"type\":\"福利\",\"url\":\"http://ww2.sinaimg.cn/large/7a8aed7bgw1eutscfcqtcj20dw0i0q4l.jpg\",\"used\":true,\"objectId\":\"55c4080240ac7d0a9507905e\",\"createdAt\":\"2015-08-07T01:21:06.112Z\",\"updatedAt\":\"2015-08-15T03:15:54.765Z\"},{\"who\":\"张涵宇\",\"publishedAt\":\"2015-08-07T03:57:47.229Z\",\"desc\":\"8.7——(2)\",\"type\":\"福利\",\"url\":\"http://ww2.sinaimg.cn/large/7a8aed7bgw1eutsd0pgiwj20go0p0djn.jpg\",\"used\":true,\"objectId\":\"55c4081d60b2c9d32a67a92e\",\"createdAt\":\"2015-08-07T01:21:33.518Z\",\"updatedAt\":\"2015-08-15T03:15:54.843Z\"}],\"休息视频\":[{\"who\":\"lxxself\",\"publishedAt\":\"2015-08-07T03:57:48.104Z\",\"desc\":\"听到就心情大好的歌,简直妖魔哈哈哈哈哈,原地址\\nhttp://v.youku.com/v_show/id_XMTQxODA5NDM2.html\",\"type\":\"休息视频\",\"url\":\"http://www.zhihu.com/question/21778055/answer/19905413?utm_source=weibo&utm_medium=weibo_share&utm_content=share_answer&utm_campaign=share_button\",\"used\":true,\"objectId\":\"55c35bc960b2750766954ec3\",\"createdAt\":\"2015-08-06T13:06:17.211Z\",\"updatedAt\":\"2015-08-15T03:15:54.872Z\"}]},\"category\":[\"iOS\",\"Android\",\"瞎推荐\",\"拓展资源\",\"福利\",\"休息视频\"]}"; } ================================================ FILE: app/src/main/groovy/zhou/gank/io/util/LogKit.java ================================================ package zhou.gank.io.util; import android.util.Log; /** * Created by zzhoujay on 2015/8/22 0022. * LogKit 可以设置日志级别 */ public class LogKit { /** * Priority constant for the println method; use Log.v. */ public static final int VERBOSE = 2; /** * Priority constant for the println method; use Log.d. */ public static final int DEBUG = 3; /** * Priority constant for the println method; use Log.i. */ public static final int INFO = 4; /** * Priority constant for the println method; use Log.w. */ public static final int WARN = 5; /** * Priority constant for the println method; use Log.e. */ public static final int ERROR = 6; /** * Priority constant for the println method. */ public static final int ASSERT = 7; public static int level = VERBOSE; public static void v(String tag, String msg) { if (level <= VERBOSE) { Log.v(tag, msg); } } public static void v(String tag, String msg, Throwable e) { if (level <= VERBOSE) { Log.v(tag, msg, e); } } public static void v(String tag, Object msg) { v(tag, msg.toString()); } public static void d(String tag, String msg) { if (level <= DEBUG) { Log.d(tag, msg); } } public static void d(String tag, String msg, Throwable e) { if (level <= DEBUG) { Log.d(tag, msg, e); } } public static void d(String tag, Object msg) { d(tag, msg.toString()); } public static void i(String tag, String msg) { if (level <= INFO) { Log.i(tag, msg); } } public static void i(String tag, String msg, Throwable e) { if (level <= INFO) { Log.i(tag, msg, e); } } public static void i(String tag, Object msg) { i(tag, msg.toString()); } public static void w(String tag, String msg) { if (level <= WARN) { Log.w(tag, msg); } } public static void w(String tag, String msg, Throwable e) { if (level <= WARN) { Log.w(tag, msg, e); } } public static void w(String tag, Throwable e) { if (level <= WARN) { Log.w(tag, e); } } public static void w(String tag, Object msg) { w(tag, msg.toString()); } public static void e(String tag, String msg) { if (level <= ERROR) { Log.e(tag, msg); } } public static void e(String tag, String msg, Throwable e) { if (level <= ERROR) { Log.e(tag, msg, e); } } public static void e(String tag, Object msg) { e(tag, msg.toString()); } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/util/NetworkKit.groovy ================================================ package zhou.gank.io.util import com.squareup.okhttp.Request import groovy.transform.CompileStatic import zhou.gank.io.App import zhou.gank.io.model.Result import zhou.gank.io.net.NetworkManager @CompileStatic class NetworkKit { static void time(int year, int month, int day, Closure closure) { Request request = new Request.Builder().get().url("$App.TIME_URL/$year/$month/$day").build() NetworkManager.getInstance().requestString(request, { result -> try{ closure(JsonKit.generate(result as String, App.getInstance().getGson())) }catch (Exception e){ LogKit.d("JsonKit","error",e) closure() } }) } static void type(String type, int pageSize, int pageNo, Closure closure) { Request request = new Request.Builder().get().url("$App.TYPE_URL/$type/$pageSize/$pageNo").build() NetworkManager.getInstance().request(request, closure, Result.class) } static void random(String type, int size, Closure closure) { Request request = new Request.Builder().get().url("$App.RANDOM_URL/$type/$size").build() NetworkManager.getInstance().request(request, closure, Result.class) } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/util/Notifier.groovy ================================================ package zhou.gank.io.util; import groovy.transform.CompileStatic @CompileStatic public interface Notifier { void notice(int noticeId) } ================================================ FILE: app/src/main/groovy/zhou/gank/io/util/NumKit.groovy ================================================ package zhou.gank.io.util; import groovy.transform.CompileStatic @CompileStatic public class NumKit { public static int getNum(String str, int num) { def gg try { gg = Integer.valueOf(str) } catch (Exception e) { gg = num } return gg } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/util/Pageable.groovy ================================================ package zhou.gank.io.util import groovy.transform.CompileStatic; @CompileStatic class Pageable implements Serializable { public int pageNo public int pageSize Pageable(int pageNo, int pageSize) { this.pageNo = pageNo this.pageSize = pageSize } Pageable() { } def next() { pageNo++ } def prev() { pageNo-- } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/util/TextKit.groovy ================================================ package zhou.gank.io.util import android.graphics.Typeface import android.text.SpannableStringBuilder import android.text.Spanned import android.text.style.ForegroundColorSpan import android.text.style.StyleSpan import groovy.transform.CompileStatic import zhou.gank.io.model.Gank import zhou.gank.io.ui.weiget.URLSpanNoUnderline; @CompileStatic class TextKit { public static SpannableStringBuilder generate(List ganHuos, int color) { SpannableStringBuilder builder = new SpannableStringBuilder(); int start; for (Gank gh : ganHuos) { start = builder.length(); builder.append(" • "); builder.setSpan(new StyleSpan(Typeface.BOLD), start, builder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); start = builder.length(); builder.append(gh.desc); builder.setSpan(new URLSpanNoUnderline(gh.url), start, builder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); builder.setSpan(new ForegroundColorSpan(color), start, builder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); builder.append("("); builder.append(gh.who); builder.append(")\n"); } return builder; } public static CharSequence getInfo() { SpannableStringBuilder builder = new SpannableStringBuilder(); int start = 0 builder.append("Gank.IO Android 客户端\n") builder.setSpan(new StyleSpan(Typeface.BOLD), start, builder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) builder.append("项目地址:") start = builder.length() builder.append("git@osc") builder.setSpan(new URLSpanNoUnderline("https://git.oschina.net/zzhoujay/Gank4Android"), start, builder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) builder.append("、") start = builder.length() builder.append("github") builder.setSpan(new URLSpanNoUnderline("https://github.com/zzhoujay/Gank4Android"), start, builder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) builder.append("\n") start = builder.length() builder.append("by zzhoujay") builder.setSpan(new StyleSpan(Typeface.ITALIC), start, builder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) return builder } } ================================================ FILE: app/src/main/groovy/zhou/gank/io/util/TimeKit.groovy ================================================ package zhou.gank.io.util; import groovy.transform.CompileStatic import java.text.SimpleDateFormat @CompileStatic public class TimeKit { public static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日") public static String format(Date data) { simpleDateFormat.format(data) } public static List getTime() { Calendar calendar = Calendar.getInstance() return [calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH)] } public static boolean future(int year, int month, int day) { Calendar now = Calendar.getInstance() Calendar calendar = Calendar.getInstance() calendar.set(Calendar.YEAR, year) calendar.set(Calendar.MONTH, month - 1) calendar.set(Calendar.DAY_OF_MONTH, day) return calendar.compareTo(now) >= 0 } } ================================================ FILE: app/src/main/res/drawable/ic_bookmark_48px.xml ================================================ ================================================ FILE: app/src/main/res/drawable/ic_cloud_off_48px.xml ================================================ ================================================ FILE: app/src/main/res/drawable/ic_cloud_queue_48px.xml ================================================ ================================================ FILE: app/src/main/res/drawable/ic_dashboard_48px.xml ================================================ ================================================ FILE: app/src/main/res/drawable/ic_event_48px.xml ================================================ ================================================ FILE: app/src/main/res/drawable/ic_extension_48px.xml ================================================ ================================================ FILE: app/src/main/res/drawable/ic_favorite_white_48px.xml ================================================ ================================================ FILE: app/src/main/res/drawable/ic_info_48px.xml ================================================ ================================================ FILE: app/src/main/res/drawable/ic_insert_emoticon_48px.xml ================================================ ================================================ FILE: app/src/main/res/drawable/ic_menu_white_48px.xml ================================================ ================================================ FILE: app/src/main/res/drawable/ic_refresh_48px.xml ================================================ ================================================ FILE: app/src/main/res/drawable/ic_settings_black_48px.xml ================================================ ================================================ FILE: app/src/main/res/drawable/ic_view_module_48px.xml ================================================ ================================================ FILE: app/src/main/res/layout/activity_compat.xml ================================================ ================================================ FILE: app/src/main/res/layout/activity_home.xml ================================================ ================================================ FILE: app/src/main/res/layout/activity_main.xml ================================================ ================================================ FILE: app/src/main/res/layout/activity_tab.xml ================================================ ================================================ FILE: app/src/main/res/layout/activity_toolbar.xml ================================================ ================================================ FILE: app/src/main/res/layout/dialog_text.xml ================================================ ================================================ FILE: app/src/main/res/layout/fragment_daily.xml ================================================ ================================================ FILE: app/src/main/res/layout/fragment_image_page.xml ================================================ ================================================ FILE: app/src/main/res/layout/fragment_recyler_view.xml ================================================ ================================================ FILE: app/src/main/res/layout/fragment_viewpager.xml ================================================ ================================================ FILE: app/src/main/res/layout/fragment_web.xml ================================================ ================================================ FILE: app/src/main/res/layout/item_daily.xml ================================================ ================================================ FILE: app/src/main/res/layout/item_gank.xml ================================================ ================================================ FILE: app/src/main/res/layout/item_image.xml ================================================ ================================================ FILE: app/src/main/res/layout/layout_gallery.xml ================================================ ================================================ FILE: app/src/main/res/layout/layout_more.xml ================================================ ================================================ FILE: app/src/main/res/layout/nav_header.xml ================================================ ================================================ FILE: app/src/main/res/menu/drawer_view.xml ================================================ ================================================ FILE: app/src/main/res/menu/menu_pop.xml ================================================ ================================================ FILE: app/src/main/res/menu/menu_pop_add.xml ================================================ ================================================ FILE: app/src/main/res/menu/menu_pop_remove.xml ================================================ ================================================ FILE: app/src/main/res/menu/menu_web.xml ================================================ ================================================ FILE: app/src/main/res/values/colors.xml ================================================ #55000000 #fafafa @color/material_grey_800 #fde0dc #f9bdbb #f69988 #f36c60 #e84e40 #e51c23 #dd191d #d01716 #c41411 #b0120a #ff7997 #ff5177 #ff2d6f #e00032 #fce4ec #f8bbd0 #f48fb1 #f06292 #ec407a #e91e63 #d81b60 #c2185d #ad1457 #880e4f #ff80ab #ff4081 #f50057 #c51162 #f3e5f5 #e1bee7 #ce93d8 #ba68c8 #ab47bc #9c27b0 #8e24aa #7b1fa2 #6a1b9a #4a148c #ea80fc #e040fb #d500f9 #aa00ff #ede7f6 #d1c4e9 #b39ddb #9575cd #7e57c2 #673ab7 #5e35b1 #512da8 #4527a0 #311b92 #b388ff #7c4dff #651fff #6200ea #e8eaf6 #c5cae9 #9fa8da #7986cb #5c5bc0 #3f51b5 #3949ab #303f9f #283593 #1a237e #8c9eff #536dfe #3d5afe #304ffe #e7e9fd #d0d9ff #afbfff #91a7ff #738ffe #5677fc #4e6cef #455ede #3b50ce #2a36b1 #a6baff #6889ff #4d73ff #4d69ff #e1f5fe #b3e5fc #81d4fa #4fc3f7 #29b6f6 #03a9f4 #039be5 #0288d1 #0277bd #01579b #80d8ff #40c4ff #00b0ff #0091ea #e0f7fa #b2ebf2 #80deea #4dd0e1 #26c6da #00bcd4 #00acc1 #0097a7 #00838f #006064 #84ffff #18ffff #00e5ff #00b8d4 #e0f2f1 #b2dfdb #80cbc4 #4db6ac #26a69a #009688 #00897b #00796b #00695c #004d40 #a7ffeb #64ffda #1de9b6 #00bfa5 #d0f8ce #a3e9a4 #72d572 #42bd41 #2baf2b #259b24 #0a8f08 #0a7e07 #056f00 #0d5302 #a2f78d #5af158 #14e715 #12c700 #f1f8e9 #dcedc8 #c5e1a5 #aed581 #9ccc65 #8bc34a #7cb342 #689f38 #558b2f #33691e #ccff90 #b2ff59 #76ff03 #64dd17 #f9fbe7 #f0f4c3 #e6ee9c #dce775 #d4e157 #cddc39 #c0ca33 #afb42b #9e9d24 #827717 #f4ff81 #eeff41 #c6ff00 #aeea00 #fffde7 #fff9c4 #fff59d #fff176 #ffee58 #ffeb3b #fdd835 #fbc02d #f9a825 #f57f17 #ffff8d #ffff00 #ffea00 #ffd600 #fff8e1 #ffecb3 #ffe082 #ffd54f #ffca28 #ffc107 #ffb300 #ffa000 #ff8f00 #ff6f00 #ffe57f #ffd740 #ffc400 #ffab00 #fff3e0 #ffe0b2 #ffcc80 #ffb74d #ffa726 #ff9800 #fb8c00 #f57c00 #ef6c00 #e65100 #ffd180 #ffab40 #ff9100 #ff6d00 #fbe9e7 #ffccbc #ffab91 #ff8a65 #ff7043 #ff5722 #f4511e #e64a19 #d84315 #bf360c #ff9e80 #ff6e40 #ff3d00 #dd2c00 #efebe9 #d7ccc8 #bcaaa4 #a1887f #8d6e63 #795548 #6d4c41 #5d4037 #4e342e #3e2723 #fafafa #f5f5f5 #eeeeee #e0e0e0 #dbdbdb #9e9e9e #757575 #616161 #424242 #212121 #000000 #ffffff #eceff1 #cfd8dc #b0bec5 #90a4ae #78909c #607d8b #546e7a #455a64 #37474f #263238 #000000 #ffffff #c5cae9 #3f51b5 #303f9f #ff80ab #ff4081 #f50057 #000000 #1e000000 #42000000 #89000000 #dd000000 #ffffff #1effffff #42ffffff #89ffffff #ddffffff ================================================ FILE: app/src/main/res/values/dimens.xml ================================================ 16dp 500dp 112sp 56sp 45sp 34sp 24sp 21sp 17sp 15sp 13sp 15sp ================================================ FILE: app/src/main/res/values/strings.xml ================================================ 干货集中营 加载中。。。 没有数据! 出错了! 刷新 今天没有干货哦 加载干货失败。。。点击重新加载 图片加载失败,点击重试 iOS Android 瞎推荐 拓展资源 福利 休息视频 今天 试试运气 收藏 zzhoujay 测试标题。。。。。。。。。。。。。。。 2015年9月23日 更新成功 网络连接不可用,请检查网络连接后重试 获取数据失败 加载中。。。 分类浏览 操作 设置 关于 key_theme 主题 key_open 使用内置浏览器打开链接 使用系统浏览器打开链接 基本 操作 清除缓存 key_clear 选择主题 在本应用内打开链接 设置 复制链接 在浏览器中打开 收藏 链接已复制到剪贴板 缓存清除成功 key_num 显示数量 每页显示的数量 分享 保存 获取图片失败 确定 已经收藏过了 收藏失败 收藏成功 取消收藏 取消收藏失败 取消收藏成功 没有更多数据了 加载失败,点击重新加载 删除 删除成功 亮主题 暗主题 Gank @string/light @string/dark light dark 10 20 30 ================================================ FILE: app/src/main/res/values/styles.xml ================================================ ================================================ FILE: app/src/main/res/values-en/strings.xml ================================================ Gank.IO confirm Error! The network connection is not available, please check the network connection and try again Failed to get the data Loading… Collect Copy the URL Open in browser Setting Category Empty! Theme Cache to clear success The URL is copied to the clipboard Clear the cache The number of each page show Using the system browser open the link Use the built-in browser open the link Base Number In this application to open URL Option Setting Select Theme Light Dark Today About Option Luck - - Cancel the collection Collection There is no more data Delete Failure load images, click retry Cancel the collection failure Collection operation fails Failed to get the picture Collection has been made Save Share Collection of success Deleted successfully Cancel the collection success Failure load, click reload Refresh Update successful ================================================ FILE: app/src/main/res/values-v21/styles.xml ================================================ ================================================ FILE: app/src/main/res/xml/setting.xml ================================================ ================================================ FILE: build.gradle ================================================ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:1.3.0' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { jcenter() } } ================================================ FILE: gradle/wrapper/gradle-wrapper.properties ================================================ #Tue Sep 22 23:09:57 GMT+08:00 2015 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists distributionUrl=https\://services.gradle.org/distributions/gradle-2.4-all.zip ================================================ FILE: gradle.properties ================================================ # Project-wide Gradle settings. # IDE (e.g. Android Studio) users: # Gradle settings configured through the IDE *will override* # any settings specified in this file. # For more details on how to configure your build environment visit # http://www.gradle.org/docs/current/userguide/build_environment.html # Specifies the JVM arguments used for the daemon process. # The setting is particularly useful for tweaking memory settings. # Default value: -Xmx10248m -XX:MaxPermSize=256m # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 # When configured, Gradle will run in incubating parallel mode. # This option should only be used with decoupled projects. More details, visit # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects # org.gradle.parallel=true ================================================ FILE: gradlew ================================================ #!/usr/bin/env bash ############################################################################## ## ## Gradle start up script for UN*X ## ############################################################################## # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. DEFAULT_JVM_OPTS="" APP_NAME="Gradle" APP_BASE_NAME=`basename "$0"` # Use the maximum available, or set MAX_FD != -1 to use that value. MAX_FD="maximum" warn ( ) { echo "$*" } die ( ) { echo echo "$*" echo exit 1 } # OS specific support (must be 'true' or 'false'). cygwin=false msys=false darwin=false case "`uname`" in CYGWIN* ) cygwin=true ;; Darwin* ) darwin=true ;; MINGW* ) msys=true ;; esac # For Cygwin, ensure paths are in UNIX format before anything is touched. if $cygwin ; then [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` fi # Attempt to set APP_HOME # Resolve links: $0 may be a link PRG="$0" # Need this for relative symlinks. while [ -h "$PRG" ] ; do ls=`ls -ld "$PRG"` link=`expr "$ls" : '.*-> \(.*\)$'` if expr "$link" : '/.*' > /dev/null; then PRG="$link" else PRG=`dirname "$PRG"`"/$link" fi done SAVED="`pwd`" cd "`dirname \"$PRG\"`/" >&- APP_HOME="`pwd -P`" cd "$SAVED" >&- CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar # Determine the Java command to use to start the JVM. if [ -n "$JAVA_HOME" ] ; then if [ -x "$JAVA_HOME/jre/sh/java" ] ; then # IBM's JDK on AIX uses strange locations for the executables JAVACMD="$JAVA_HOME/jre/sh/java" else JAVACMD="$JAVA_HOME/bin/java" fi if [ ! -x "$JAVACMD" ] ; then die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME Please set the JAVA_HOME variable in your environment to match the location of your Java installation." fi else JAVACMD="java" which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. Please set the JAVA_HOME variable in your environment to match the location of your Java installation." fi # Increase the maximum file descriptors if we can. if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then MAX_FD_LIMIT=`ulimit -H -n` if [ $? -eq 0 ] ; then if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then MAX_FD="$MAX_FD_LIMIT" fi ulimit -n $MAX_FD if [ $? -ne 0 ] ; then warn "Could not set maximum file descriptor limit: $MAX_FD" fi else warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" fi fi # For Darwin, add options to specify how the application appears in the dock if $darwin; then GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" fi # For Cygwin, switch paths to Windows format before running java if $cygwin ; then APP_HOME=`cygpath --path --mixed "$APP_HOME"` CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` # We build the pattern for arguments to be converted via cygpath ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` SEP="" for dir in $ROOTDIRSRAW ; do ROOTDIRS="$ROOTDIRS$SEP$dir" SEP="|" done OURCYGPATTERN="(^($ROOTDIRS))" # Add a user-defined pattern to the cygpath arguments if [ "$GRADLE_CYGPATTERN" != "" ] ; then OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" fi # Now convert the arguments - kludge to limit ourselves to /bin/sh i=0 for arg in "$@" ; do CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` else eval `echo args$i`="\"$arg\"" fi i=$((i+1)) done case $i in (0) set -- ;; (1) set -- "$args0" ;; (2) set -- "$args0" "$args1" ;; (3) set -- "$args0" "$args1" "$args2" ;; (4) set -- "$args0" "$args1" "$args2" "$args3" ;; (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; esac fi # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules function splitJvmOpts() { JVM_OPTS=("$@") } eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" ================================================ FILE: gradlew.bat ================================================ @if "%DEBUG%" == "" @echo off @rem ########################################################################## @rem @rem Gradle startup script for Windows @rem @rem ########################################################################## @rem Set local scope for the variables with windows NT shell if "%OS%"=="Windows_NT" setlocal @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. set DEFAULT_JVM_OPTS= set DIRNAME=%~dp0 if "%DIRNAME%" == "" set DIRNAME=. set APP_BASE_NAME=%~n0 set APP_HOME=%DIRNAME% @rem Find java.exe if defined JAVA_HOME goto findJavaFromJavaHome set JAVA_EXE=java.exe %JAVA_EXE% -version >NUL 2>&1 if "%ERRORLEVEL%" == "0" goto init echo. echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. echo. echo Please set the JAVA_HOME variable in your environment to match the echo location of your Java installation. goto fail :findJavaFromJavaHome set JAVA_HOME=%JAVA_HOME:"=% set JAVA_EXE=%JAVA_HOME%/bin/java.exe if exist "%JAVA_EXE%" goto init echo. echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% echo. echo Please set the JAVA_HOME variable in your environment to match the echo location of your Java installation. goto fail :init @rem Get command-line arguments, handling Windowz variants if not "%OS%" == "Windows_NT" goto win9xME_args if "%@eval[2+2]" == "4" goto 4NT_args :win9xME_args @rem Slurp the command line arguments. set CMD_LINE_ARGS= set _SKIP=2 :win9xME_args_slurp if "x%~1" == "x" goto execute set CMD_LINE_ARGS=%* goto execute :4NT_args @rem Get arguments from the 4NT Shell from JP Software set CMD_LINE_ARGS=%$ :execute @rem Setup the command line set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar @rem Execute Gradle "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% :end @rem End local scope for the variables with windows NT shell if "%ERRORLEVEL%"=="0" goto mainEnd :fail rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of rem the _cmd.exe /c_ return code! if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 exit /b 1 :mainEnd if "%OS%"=="Windows_NT" endlocal :omega ================================================ FILE: settings.gradle ================================================ include ':app'