Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.annotation.SuppressLint;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.content.res.XmlResourceParser;
import android.net.Uri;

import android.os.Bundle;
import android.text.TextUtils;
import java.lang.reflect.Method;

public class Main {
    public static final String TARGET_SCHEME_URI = "JM_SPIDER_TARGET_SCHEME_URL";
    private static String homeScheme = null;

    @SuppressLint("NewApi")
    public static void sendSchemeForResultAddMediaStack(Fragment activity, String url, int requestCode) {
        sendSchemeForResultAddMediaStack(activity, url, getHomeScheme(activity.getActivity()), requestCode);
    }

    public static void sendSchemeForResultAddMediaStack(Activity activity, String url, int requestCode) {
        sendSchemeForResultAddMediaStack(activity, url, getHomeScheme(activity), requestCode);
    }

    public static void sendSchemeForResultAddMediaStack(Fragment activity, String mediaActivityUrl, String url,
            int requestCode) {
        sendSchemeForResultAddMediaStack((Fragment) activity, mediaActivityUrl, url, requestCode, (Bundle) null);
    }

    public static void sendSchemeForResultAddMediaStack(Activity activity, String mediaActivityUrl, String url,
            int requestCode) {
        sendSchemeForResultAddMediaStack((Activity) activity, mediaActivityUrl, url, requestCode, (Bundle) null);
    }

    @SuppressLint("NewApi")
    public static void sendSchemeForResultAddMediaStack(Fragment activity, String mediaActivityUrl, String url,
            int requestCode, Bundle bundle) {
        Intent intent = new Intent("android.intent.action.VIEW", Uri.parse(mediaActivityUrl));
        intent.putExtra(TARGET_SCHEME_URI, url);
        if (bundle != null) {
            intent.putExtras(bundle);
        }

        activity.startActivityForResult(intent, requestCode);
    }

    public static void sendSchemeForResultAddMediaStack(Activity context, String mediaActivityUrl, String url,
            int requestCode, Bundle bundle) {
        Intent intent = new Intent("android.intent.action.VIEW", Uri.parse(mediaActivityUrl));
        intent.addFlags(67108864);
        context.startActivity(intent);
        intent = new Intent("android.intent.action.VIEW", Uri.parse(url));
        if (bundle != null) {
            intent.putExtras(bundle);
        }

        context.startActivityForResult(intent, requestCode);
    }

    public static String getHomeScheme(Context ctx) {
        if (TextUtils.isEmpty(homeScheme)) {
            homeScheme = getMetaData(ctx.getApplicationContext(), "MAIN_SCHEME");
        }

        return homeScheme;
    }

    public static String getMetaData(Context app, String name) {
        Bundle bundle = app.getApplicationInfo().metaData;
        if (bundle != null) {
            return bundle.getString(name);
        } else {
            XmlResourceParser parser = null;
            AssetManager assmgr = null;

            try {
                assmgr = (AssetManager) AssetManager.class.newInstance();
                Method e = AssetManager.class.getDeclaredMethod("addAssetPath", new Class[] { String.class });
                e.setAccessible(true);
                int cookie = ((Integer) e.invoke(assmgr, new Object[] { app.getApplicationInfo().sourceDir }))
                        .intValue();
                if (cookie != 0) {
                    String ANDROID_RESOURCES = "http://schemas.android.com/apk/res/android";
                    parser = assmgr.openXmlResourceParser(cookie, "AndroidManifest.xml");
                    boolean findAppMetadata = false;
                    int event = parser.getEventType();
                    while (event != 1) {
                        switch (event) {
                        case 2:
                            String nodeName = parser.getName();
                            String metadataName;
                            if ("meta-data".equals(nodeName)) {
                                findAppMetadata = true;
                                metadataName = parser.getAttributeValue(ANDROID_RESOURCES, "name");
                                if (metadataName.equals(name)) {
                                    String var12 = parser.getAttributeValue(ANDROID_RESOURCES, "value");
                                    return var12;
                                }
                            } else if (findAppMetadata) {
                                metadataName = null;
                                return metadataName;
                            }
                        default:
                            event = parser.next();
                        }
                    }
                }
            } catch (Throwable var16) {
                var16.printStackTrace();
            } finally {
                if (parser != null) {
                    parser.close();
                }

                if (assmgr != null) {
                    assmgr.close();
                }

            }

            return null;
        }
    }
}