Categories
discuss

Register a new file type in Android

I want to write a simple STL (geometrical data file) viewer application on Android, but I’m not able to make recognize a format to the system. I wrote this in my app manifest file:

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <action android:name="android.intent.action.EDIT" />
    <action android:name="android.intent.action.PICK" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:scheme="http" />
    <data android:pathPattern=".*\.stl" />
    <data android:mimeType="application/sla" />
    <data android:host="*" />
</intent-filter>

But the moment I launch the browser and go to download some sample STL file, the download is interrupted and I’m reported that the data file type is unknown for the system.

I have no real Android device, so I use only an emulator, and for development I use C# on MonoDroid (but I honestly don’t think that is the problem).

How could I fix this problem?

Answer

I’m using this manifest to register (for example) a .stl file type with my application:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.test.core" android:versionCode="1" android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Testy" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name="ThorActivity" android:label="@string/app_name">
        </activity>

        <activity android:name="LokiActivity" android:label="@string/app_name">
        </activity>

        <activity android:name="OdinActivity" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="http" android:host="*"
                    android:pathPattern=".*\.stl" />
                <data android:scheme="https" android:host="*"
                    android:pathPattern=".*\.stl" />
                <data android:scheme="content" android:host="*"
                    android:pathPattern=".*\.stl" />
                <data android:scheme="file" android:host="*"
                    android:pathPattern=".*\.stl" />
            </intent-filter>
        </activity>
    </application>

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

</manifest>

As you can see, I’m linking the .stl file extension to the activity OdinActivity. Inside the OdinActivity, I use the following line to get the file path so I can open it:

filePath = getIntent().getData().getEncodedPath();

Then I just open it to read from it:

FileOutputStream out = new FileOutputStream(new File(filePath));
Categories
discuss

Does JavaScript (ECMAScript5) Strict Mode offer significant performance advantages to merit widespread use?

I’m reading up a bit on using Strict Mode for JavaScript and it seems that, generally speaking, the idea is to force a more rigid set of rules onto the coder to ensure that the JS engine can optimise the code better. It almost feels like the JavaScript equivalent of “Option Explicit” in Visual Basic.

If this is basically the net effect of applying Strict Mode to my code, would the performance difference be such that it would be worth applying out of habit rather than case-by-case? Are there other advantages besides code stability that might be worth considering?

What are some of the key reasons I would want to apply Strict Mode to my scripts?

Answer

Well, strict mode code can certainly perform better because it removes issues that made optimization harder, for example, from the top of my head:

  • The with statement was removed (Really difficult -if not impossible- to optimize).
  • No more undeclared assignments, and other prohibitions, e.g. (delete varName;)
  • eval does not introduce variable/function declarations into the local scope.
  • arguments.callee was removed, (difficult to optimize (e.g. function inlining))
  • The arguments object index named properties are not anymore dynamically mapped to the named formal parameters.
Categories
discuss

Android Changing image size depending on Screen Size?

So I need to change the size of an image depending on the area of the screen. The image will have to be half of the screen height, because otherwise it overlaps some text.

So Height= 1/2 Screen Height. Width = Height*Aspect Ratio (Just trying to keep the aspect ratio the same)

I found something that was:

Display myDisplay = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width =myDisplay.getWidth();
int height=myDisplay.getHeight();

But how would I change image height in java? or even XML if possible? I can’t seem to find a working answer.

Answer

You can do this with LayoutParams in code. Unfortunately there’s no way to specify percentages through XML (not directly, you can mess around with weights, but that’s not always going to help, and it won’t keep your aspect ratio), but this should work for you:

//assuming your layout is in a LinearLayout as its root
LinearLayout layout = (LinearLayout)findViewById(R.id.rootlayout);

ImageView image = new ImageView(this);
image.setImageResource(R.drawable.image);

int newHeight = getWindowManager().getDefaultDisplay().getHeight() / 2;
int orgWidth = image.getDrawable().getIntrinsicWidth();
int orgHeight = image.getDrawable().getIntrinsicHeight();

//double check my math, this should be right, though
int newWidth = Math.floor((orgWidth * newHeight) / orgHeight);

//Use RelativeLayout.LayoutParams if your parent is a RelativeLayout
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
    newWidth, newHeight);
image.setLayoutParams(params);
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
layout.addView(image);

Might be overcomplicated, maybe there’s an easier way? This is what I’d first try, though.

Categories
discuss

How do I create a panel of buttons with a simple divider line, or no dividers, in Android?

This seems like a really basic task, but after a lot of searching and research, still haven’t found a clear answer. I found GridView, but not sure this is how you’re supposed to do it.

I’ve found several apps that have UI elements similar to what I need, for example the top buttons of the android market.

here's an example of what I'm talking about

Answer

That can easily be accomplished with a horizontal LinearLayout that contains a set of Buttons or ImageButtons. The rest is just styling.

See Hello Linear Layout to get started.

Categories
discuss

What is the point of a Facade in Java EE?

I’m not really understanding the point of a facade.

public abstract class AbstractFacade<T> {

    private Class<T> entityClass;

    public AbstractFacade(Class<T> entityClass) {
        this.entityClass = entityClass;
    }

    protected abstract EntityManager getEntityManager();

    public void create(T entity) {
        getEntityManager().persist(entity);
    }

    public void edit(T entity) {
        getEntityManager().merge(entity);
    }

    public void remove(T entity) {
        getEntityManager().remove(getEntityManager().merge(entity));
    }

    public T find(Object id) {
        return getEntityManager().find(entityClass, id);
    }

    public List<T> findAll() {
        CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        return getEntityManager().createQuery(cq).getResultList();
    }

    public List<T> findRange(int[] range) {
        CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        Query q = getEntityManager().createQuery(cq);
        q.setMaxResults(range[1] - range[0]);
        q.setFirstResult(range[0]);
        return q.getResultList();
    }

    public int count() {
        CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        Root<T> rt = cq.from(entityClass);
        cq.select(getEntityManager().getCriteriaBuilder().count(rt));
        Query q = getEntityManager().createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();
    }
}

If I have this code and then I have an EJB like this.

@Stateless
public class WrapSpecFacade extends AbstractFacade<WrapSpec> {
    @PersistenceContext
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public WrapSpecFacade() {
        super(WrapSpec.class);
    }

}

What is the point of this? Why call this a facade? To me it’s just an abstract class that groups similar functionality. Thanks.

Answer

Facade is a design pattern. A pattern, a software pattern, is a set of rules in order to organize code and provide a certain structure to it. Some goals can be reached by using a pattern. A design pattern is used when designing the application.

The Facade pattern allows programmers to create a simple interface for objects to use other objects. Consider working with a very complex group of classes, all implementing their own interfaces. Well, you want to provide an interface to expose only some functionality of the many you have. By doing so, you achieve code simplicity, flexibility, integration and loose-coupling.

Facade, in your example, is used in order to manage coupling between many actors. It is a design issue. When you have many components interacting together, the more they are tied the harder it will be to maintain them (I mean code maintenance). Facade allows you to reach loose coupling, which is a goal a programmer should always try to reach.

Consider the following:

public class MyClass1 implements Interface1 {
   public void call1() {}
   public call call2() {}
}

public class MyClass2 implements Interface2 {
   public void call3() {}
   public void call4() {}
}

public class MyClass {
   private MyClass1 a;
   private MyClass2 b;
   //calling methods call1 call2 call3 and call4 in other methods of this class
   ...
   ...
}

If you had to change business logic located in a class used by call1 or call2… by not changing the interface, you would not need to change all these classes, but just the class inside the method used by one of the interface methods of the first two classes.

Facade lets you improve this mechanism.

I am sorry but I realize that it does not look so wonderful. Design patterns are heavily used in the software industry and they can be very useful when working on large projects. You might point out that your project is not that large and that may be true, but Java EE aims to help business and enterprise-level application programming. That’s why sometimes the facade pattern is used by default (some IDEs use it too).

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..