|
| 1 | +package com.dianping.app; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileInputStream; |
| 5 | +import java.util.Locale; |
| 6 | + |
| 7 | +import org.json.JSONObject; |
| 8 | + |
| 9 | +import android.app.Application; |
| 10 | +import android.content.Intent; |
| 11 | +import android.net.Uri; |
| 12 | +import android.text.TextUtils; |
| 13 | +import android.util.Log; |
| 14 | + |
| 15 | +import com.dianping.loader.LoaderActivity; |
| 16 | +import com.dianping.loader.MainActivity; |
| 17 | +import com.dianping.loader.MyClassLoader; |
| 18 | +import com.dianping.loader.RepositoryManager; |
| 19 | +import com.dianping.loader.model.FileSpec; |
| 20 | +import com.dianping.loader.model.FragmentSpec; |
| 21 | +import com.dianping.loader.model.SiteSpec; |
| 22 | + |
| 23 | +public class MyApplication extends Application { |
| 24 | + public static final String PRIMARY_SCHEME = "app"; |
| 25 | + |
| 26 | + private static MyApplication instance; |
| 27 | + private RepositoryManager repoManager; |
| 28 | + |
| 29 | + public static MyApplication instance() { |
| 30 | + if (instance == null) { |
| 31 | + throw new IllegalStateException("Application has not been created"); |
| 32 | + } |
| 33 | + |
| 34 | + return instance; |
| 35 | + } |
| 36 | + |
| 37 | + public MyApplication() { |
| 38 | + instance = this; |
| 39 | + } |
| 40 | + |
| 41 | + public RepositoryManager repositoryManager() { |
| 42 | + if (repoManager == null) { |
| 43 | + repoManager = new RepositoryManager(this); |
| 44 | + } |
| 45 | + return repoManager; |
| 46 | + } |
| 47 | + |
| 48 | + public SiteSpec readSite() { |
| 49 | + File dir = new File(getFilesDir(), "repo"); |
| 50 | + File local = new File(dir, "site.txt"); |
| 51 | + if (local.length() > 0) { |
| 52 | + try { |
| 53 | + FileInputStream fis = new FileInputStream(local); |
| 54 | + byte[] bytes = new byte[fis.available()]; |
| 55 | + int l = fis.read(bytes); |
| 56 | + fis.close(); |
| 57 | + String str = new String(bytes, 0, l, "UTF-8"); |
| 58 | + JSONObject json = new JSONObject(str); |
| 59 | + return new SiteSpec(json); |
| 60 | + } catch (Exception e) { |
| 61 | + Log.w("loader", "fail to load site.txt from " + local, e); |
| 62 | + } |
| 63 | + } |
| 64 | + return new SiteSpec("empty.0", "0", new FileSpec[0], |
| 65 | + new FragmentSpec[0]); |
| 66 | + } |
| 67 | + |
| 68 | + public Intent urlMap(Intent intent) { |
| 69 | + do { |
| 70 | + // already specify a class, no need to map url |
| 71 | + if (intent.getComponent() != null) |
| 72 | + break; |
| 73 | + |
| 74 | + // only process my scheme uri |
| 75 | + Uri uri = intent.getData(); |
| 76 | + if (uri == null) |
| 77 | + break; |
| 78 | + if (uri.getScheme() == null) |
| 79 | + break; |
| 80 | + if (!(PRIMARY_SCHEME.equalsIgnoreCase(uri.getScheme()))) |
| 81 | + break; |
| 82 | + |
| 83 | + SiteSpec site = null; |
| 84 | + if (intent.hasExtra("_site")) { |
| 85 | + site = intent.getParcelableExtra("_site"); |
| 86 | + } |
| 87 | + if (site == null) { |
| 88 | + site = readSite(); |
| 89 | + intent.putExtra("_site", site); |
| 90 | + } |
| 91 | + |
| 92 | + // i'm responsible |
| 93 | + intent.setClass(this, LoaderActivity.class); |
| 94 | + |
| 95 | + String host = uri.getHost(); |
| 96 | + if (TextUtils.isEmpty(host)) |
| 97 | + break; |
| 98 | + host = host.toLowerCase(Locale.US); |
| 99 | + FragmentSpec fragment = site.getFragment(host); |
| 100 | + if (fragment == null) |
| 101 | + break; |
| 102 | + intent.putExtra("_fragment", fragment.name()); |
| 103 | + |
| 104 | + // class loader |
| 105 | + ClassLoader classLoader; |
| 106 | + if (TextUtils.isEmpty(fragment.code())) { |
| 107 | + classLoader = getClassLoader(); |
| 108 | + } else { |
| 109 | + intent.putExtra("_code", fragment.code()); |
| 110 | + FileSpec fs = site.getFile(fragment.code()); |
| 111 | + if (fs == null) |
| 112 | + break; |
| 113 | + classLoader = MyClassLoader.getClassLoader(site, fs); |
| 114 | + if (classLoader == null) |
| 115 | + break; |
| 116 | + } |
| 117 | + |
| 118 | + intent.setClass(this, MainActivity.class); |
| 119 | + } while (false); |
| 120 | + |
| 121 | + return intent; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public void startActivity(Intent intent) { |
| 126 | + intent = urlMap(intent); |
| 127 | + super.startActivity(intent); |
| 128 | + } |
| 129 | + |
| 130 | +} |
0 commit comments