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.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 {
    private static String homeScheme = null;

    @SuppressLint("NewApi")
    public static void sendSchemeAddMediaStack(Fragment context, String url) {
        sendSchemeAddMediaStack(context, url, getHomeScheme(context.getActivity()));
    }

    public static void sendSchemeAddMediaStack(Context context, String url) {
        sendSchemeAddMediaStack(context, url, getHomeScheme(context));
    }

    @SuppressLint("NewApi")
    public static void sendSchemeAddMediaStack(Fragment context, String url, Bundle bundle) {
        sendSchemeAddMediaStack(context, getHomeScheme(context.getActivity()), url, bundle);
    }

    public static void sendSchemeAddMediaStack(Context context, String url, Bundle bundle) {
        sendSchemeAddMediaStack(context, getHomeScheme(context), url, bundle);
    }

    public static void sendSchemeAddMediaStack(Fragment context, String mediaActivityUrl, String url) {
        sendSchemeAddMediaStack((Fragment) context, mediaActivityUrl, url, (Bundle) null);
    }

    public static void sendSchemeAddMediaStack(Context context, String mediaActivityUrl, String url) {
        sendSchemeAddMediaStack((Context) context, mediaActivityUrl, url, (Bundle) null);
    }

    @SuppressLint("NewApi")
    public static void sendSchemeAddMediaStack(Fragment context, String mediaActivityUrl, String url,
            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.startActivity(intent);
    }

    public static void sendSchemeAddMediaStack(Context context, String mediaActivityUrl, String url,
            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.startActivity(intent);
    }

    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;
        }
    }
}