15b539461dcc159bd89297443780d635ccc5e3564John Hoford/*
25b539461dcc159bd89297443780d635ccc5e3564John Hoford * Copyright (C) 2015 The Android Open Source Project
35b539461dcc159bd89297443780d635ccc5e3564John Hoford *
45b539461dcc159bd89297443780d635ccc5e3564John Hoford * Licensed under the Apache License, Version 2.0 (the "License");
55b539461dcc159bd89297443780d635ccc5e3564John Hoford * you may not use this file except in compliance with the License.
65b539461dcc159bd89297443780d635ccc5e3564John Hoford * You may obtain a copy of the License at
75b539461dcc159bd89297443780d635ccc5e3564John Hoford *
85b539461dcc159bd89297443780d635ccc5e3564John Hoford *      http://www.apache.org/licenses/LICENSE-2.0
95b539461dcc159bd89297443780d635ccc5e3564John Hoford *
105b539461dcc159bd89297443780d635ccc5e3564John Hoford * Unless required by applicable law or agreed to in writing, software
115b539461dcc159bd89297443780d635ccc5e3564John Hoford * distributed under the License is distributed on an "AS IS" BASIS,
125b539461dcc159bd89297443780d635ccc5e3564John Hoford * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135b539461dcc159bd89297443780d635ccc5e3564John Hoford * See the License for the specific language governing permissions and
145b539461dcc159bd89297443780d635ccc5e3564John Hoford * limitations under the License.
155b539461dcc159bd89297443780d635ccc5e3564John Hoford */
165b539461dcc159bd89297443780d635ccc5e3564John Hoford
175b539461dcc159bd89297443780d635ccc5e3564John Hofordpackage com.example.android.rs.vr;
185b539461dcc159bd89297443780d635ccc5e3564John Hoford
195b539461dcc159bd89297443780d635ccc5e3564John Hofordimport android.app.Activity;
205b539461dcc159bd89297443780d635ccc5e3564John Hofordimport android.app.ProgressDialog;
21c81b64d6800dfdb339afe8ad2e89af149ea92fd5John Hofordimport android.content.pm.ActivityInfo;
22c81b64d6800dfdb339afe8ad2e89af149ea92fd5John Hofordimport android.content.res.Configuration;
235b539461dcc159bd89297443780d635ccc5e3564John Hofordimport android.os.AsyncTask;
245b539461dcc159bd89297443780d635ccc5e3564John Hofordimport android.os.Bundle;
255b539461dcc159bd89297443780d635ccc5e3564John Hofordimport android.renderscript.RenderScript;
265b539461dcc159bd89297443780d635ccc5e3564John Hofordimport android.util.Log;
275b539461dcc159bd89297443780d635ccc5e3564John Hofordimport android.view.Menu;
285b539461dcc159bd89297443780d635ccc5e3564John Hofordimport android.view.MenuItem;
295b539461dcc159bd89297443780d635ccc5e3564John Hofordimport android.view.View;
305b539461dcc159bd89297443780d635ccc5e3564John Hofordimport android.view.ViewGroup;
315b539461dcc159bd89297443780d635ccc5e3564John Hofordimport android.widget.PopupMenu;
325b539461dcc159bd89297443780d635ccc5e3564John Hofordimport android.widget.TextView;
335b539461dcc159bd89297443780d635ccc5e3564John Hofordimport android.widget.ToggleButton;
345b539461dcc159bd89297443780d635ccc5e3564John Hoford
355b539461dcc159bd89297443780d635ccc5e3564John Hofordimport java.io.File;
365b539461dcc159bd89297443780d635ccc5e3564John Hoford
375b539461dcc159bd89297443780d635ccc5e3564John Hofordimport rsexample.google.com.vrdemo.R;
385b539461dcc159bd89297443780d635ccc5e3564John Hoford
395b539461dcc159bd89297443780d635ccc5e3564John Hofordimport com.example.android.rs.vr.engine.Volume;
405b539461dcc159bd89297443780d635ccc5e3564John Hofordimport com.example.android.rs.vr.engine.VrState;
415ee349cd7a75079b6bac1d8ace66455ecd1afb17John Hofordimport com.example.android.rs.vr.loaders.Droid;
425ee349cd7a75079b6bac1d8ace66455ecd1afb17John Hofordimport com.example.android.rs.vr.loaders.Mandelbulb;
435b539461dcc159bd89297443780d635ccc5e3564John Hofordimport com.example.android.rs.vr.loaders.VolumeLoader;
445b539461dcc159bd89297443780d635ccc5e3564John Hoford
455b539461dcc159bd89297443780d635ccc5e3564John Hoford/**
465b539461dcc159bd89297443780d635ccc5e3564John Hoford * basic activity loads the volume and sets it on the VrView
475b539461dcc159bd89297443780d635ccc5e3564John Hoford */
485b539461dcc159bd89297443780d635ccc5e3564John Hofordpublic class VrActivity extends Activity {
495b539461dcc159bd89297443780d635ccc5e3564John Hoford    private static final String LOGTAG = "VrActivity";
505b539461dcc159bd89297443780d635ccc5e3564John Hoford    VrState mState = new VrState();
515b539461dcc159bd89297443780d635ccc5e3564John Hoford    VrView mVrView;
525b539461dcc159bd89297443780d635ccc5e3564John Hoford    VolumeLoader mLoader;
535b539461dcc159bd89297443780d635ccc5e3564John Hoford    private RenderScript mRs;
545b539461dcc159bd89297443780d635ccc5e3564John Hoford
555b539461dcc159bd89297443780d635ccc5e3564John Hoford    @Override
565b539461dcc159bd89297443780d635ccc5e3564John Hoford    protected void onCreate(Bundle savedInstanceState) {
575b539461dcc159bd89297443780d635ccc5e3564John Hoford        super.onCreate(savedInstanceState);
585b539461dcc159bd89297443780d635ccc5e3564John Hoford        setContentView(R.layout.activity_vr);
595b539461dcc159bd89297443780d635ccc5e3564John Hoford
605b539461dcc159bd89297443780d635ccc5e3564John Hoford        mVrView = (VrView) findViewById(R.id.view);
615b539461dcc159bd89297443780d635ccc5e3564John Hoford        mRs = RenderScript.create(VrActivity.this);
625b539461dcc159bd89297443780d635ccc5e3564John Hoford
635b539461dcc159bd89297443780d635ccc5e3564John Hoford        String dir = "/sdcard/Download/volumes";
645b539461dcc159bd89297443780d635ccc5e3564John Hoford        mLoader = new VolumeLoader(dir);
655b539461dcc159bd89297443780d635ccc5e3564John Hoford        VrSetupTask setup = new VrSetupTask();
665b539461dcc159bd89297443780d635ccc5e3564John Hoford        String [] names = mLoader.getNames();
675b539461dcc159bd89297443780d635ccc5e3564John Hoford        setup.execute(names[0]);
685b539461dcc159bd89297443780d635ccc5e3564John Hoford        TextView tv = (TextView) findViewById(R.id.title);
695b539461dcc159bd89297443780d635ccc5e3564John Hoford        tv.setText(names[0]);
705b539461dcc159bd89297443780d635ccc5e3564John Hoford    }
715b539461dcc159bd89297443780d635ccc5e3564John Hoford
725b539461dcc159bd89297443780d635ccc5e3564John Hoford    class VrSetupTask extends AsyncTask<String, Integer, Volume> {
735b539461dcc159bd89297443780d635ccc5e3564John Hoford        ProgressDialog progressDialog;
745ee349cd7a75079b6bac1d8ace66455ecd1afb17John Hoford        String message;
755b539461dcc159bd89297443780d635ccc5e3564John Hoford        protected void onPreExecute() {
765b539461dcc159bd89297443780d635ccc5e3564John Hoford            super.onPreExecute();
775b539461dcc159bd89297443780d635ccc5e3564John Hoford            progressDialog = new ProgressDialog(VrActivity.this);
785ee349cd7a75079b6bac1d8ace66455ecd1afb17John Hoford            progressDialog.setMessage(message= "Loading Volume");
795b539461dcc159bd89297443780d635ccc5e3564John Hoford            progressDialog.setIndeterminate(true);
805b539461dcc159bd89297443780d635ccc5e3564John Hoford            progressDialog.setCancelable(false);
815b539461dcc159bd89297443780d635ccc5e3564John Hoford            progressDialog.setProgress(0);
825b539461dcc159bd89297443780d635ccc5e3564John Hoford            progressDialog.setMax(100);
835b539461dcc159bd89297443780d635ccc5e3564John Hoford            progressDialog.show();
845b539461dcc159bd89297443780d635ccc5e3564John Hoford
855b539461dcc159bd89297443780d635ccc5e3564John Hoford            mLoader.setProgressListener(new VolumeLoader.ProgressListener() {
865b539461dcc159bd89297443780d635ccc5e3564John Hoford                @Override
875b539461dcc159bd89297443780d635ccc5e3564John Hoford                public void progress(int n, int total) {
885b539461dcc159bd89297443780d635ccc5e3564John Hoford                     publishProgress(n, total);
895b539461dcc159bd89297443780d635ccc5e3564John Hoford                }
905b539461dcc159bd89297443780d635ccc5e3564John Hoford            });
915b539461dcc159bd89297443780d635ccc5e3564John Hoford        }
925b539461dcc159bd89297443780d635ccc5e3564John Hoford
935b539461dcc159bd89297443780d635ccc5e3564John Hoford        @Override
945b539461dcc159bd89297443780d635ccc5e3564John Hoford        protected Volume doInBackground(String... names) {
955ee349cd7a75079b6bac1d8ace66455ecd1afb17John Hoford            if (names[0].equals(Droid.NAME) || names[0].equals(Mandelbulb.NAME)) {
965ee349cd7a75079b6bac1d8ace66455ecd1afb17John Hoford                message = "Computing "+names[0]+": ";
975ee349cd7a75079b6bac1d8ace66455ecd1afb17John Hoford            } else {
985ee349cd7a75079b6bac1d8ace66455ecd1afb17John Hoford                message =" Loading " + names[0]+": ";
995ee349cd7a75079b6bac1d8ace66455ecd1afb17John Hoford            }
1005b539461dcc159bd89297443780d635ccc5e3564John Hoford            return  mLoader.getVolume(mRs, names[0]);
1015b539461dcc159bd89297443780d635ccc5e3564John Hoford        }
1025b539461dcc159bd89297443780d635ccc5e3564John Hoford
1035b539461dcc159bd89297443780d635ccc5e3564John Hoford        @Override
1045b539461dcc159bd89297443780d635ccc5e3564John Hoford        protected void onProgressUpdate(Integer... progress) {
1055ee349cd7a75079b6bac1d8ace66455ecd1afb17John Hoford            progressDialog.setMessage(message+progress[0]+"/"+progress[1]);
1065b539461dcc159bd89297443780d635ccc5e3564John Hoford            progressDialog.setMax(progress[1]);
1075b539461dcc159bd89297443780d635ccc5e3564John Hoford            progressDialog.setProgress(progress[0]);
1085b539461dcc159bd89297443780d635ccc5e3564John Hoford            Log.v(LOGTAG,"Loading "+ progress[0]+"/"+progress[1]);
1095b539461dcc159bd89297443780d635ccc5e3564John Hoford        }
1105b539461dcc159bd89297443780d635ccc5e3564John Hoford
1115b539461dcc159bd89297443780d635ccc5e3564John Hoford        protected void onPostExecute(Volume v) {
1125b539461dcc159bd89297443780d635ccc5e3564John Hoford            Log.v(LOGTAG,"done");
1135b539461dcc159bd89297443780d635ccc5e3564John Hoford            mVrView.setVolume(mRs, v);
1145b539461dcc159bd89297443780d635ccc5e3564John Hoford            progressDialog.dismiss();
115c81b64d6800dfdb339afe8ad2e89af149ea92fd5John Hoford            if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
116c81b64d6800dfdb339afe8ad2e89af149ea92fd5John Hoford                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
117c81b64d6800dfdb339afe8ad2e89af149ea92fd5John Hoford            } else {
118c81b64d6800dfdb339afe8ad2e89af149ea92fd5John Hoford                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
119c81b64d6800dfdb339afe8ad2e89af149ea92fd5John Hoford            }
1205b539461dcc159bd89297443780d635ccc5e3564John Hoford        }
1215b539461dcc159bd89297443780d635ccc5e3564John Hoford    }
1225b539461dcc159bd89297443780d635ccc5e3564John Hoford
1235b539461dcc159bd89297443780d635ccc5e3564John Hoford    public void cutXClick(View v) {
1245b539461dcc159bd89297443780d635ccc5e3564John Hoford        mVrView.setMode(VrView.CUT_X_MODE);
1255b539461dcc159bd89297443780d635ccc5e3564John Hoford        uncheckOthers(v);
1265b539461dcc159bd89297443780d635ccc5e3564John Hoford    }
1275b539461dcc159bd89297443780d635ccc5e3564John Hoford
1285b539461dcc159bd89297443780d635ccc5e3564John Hoford    public void cutYClick(View v) {
1295b539461dcc159bd89297443780d635ccc5e3564John Hoford        mVrView.setMode(VrView.CUT_Y_MODE);
1305b539461dcc159bd89297443780d635ccc5e3564John Hoford        uncheckOthers(v);
1315b539461dcc159bd89297443780d635ccc5e3564John Hoford    }
1325b539461dcc159bd89297443780d635ccc5e3564John Hoford
1335b539461dcc159bd89297443780d635ccc5e3564John Hoford    public void cutZClick(View v) {
1345b539461dcc159bd89297443780d635ccc5e3564John Hoford        mVrView.setMode(VrView.CUT_Z_MODE);
1355b539461dcc159bd89297443780d635ccc5e3564John Hoford        uncheckOthers(v);
1365b539461dcc159bd89297443780d635ccc5e3564John Hoford    }
1375b539461dcc159bd89297443780d635ccc5e3564John Hoford
1385b539461dcc159bd89297443780d635ccc5e3564John Hoford    public void rotateClick(View v) {
1395b539461dcc159bd89297443780d635ccc5e3564John Hoford        mVrView.setMode(VrView.ROTATE_MODE);
1405b539461dcc159bd89297443780d635ccc5e3564John Hoford        uncheckOthers(v);
1415b539461dcc159bd89297443780d635ccc5e3564John Hoford    }
1425b539461dcc159bd89297443780d635ccc5e3564John Hoford
1435b539461dcc159bd89297443780d635ccc5e3564John Hoford    public void resetClick(View v) {
1445b539461dcc159bd89297443780d635ccc5e3564John Hoford        mVrView.resetCut();
1455b539461dcc159bd89297443780d635ccc5e3564John Hoford    }
1465b539461dcc159bd89297443780d635ccc5e3564John Hoford
1475b539461dcc159bd89297443780d635ccc5e3564John Hoford    public void saveClick(View v) {
1485b539461dcc159bd89297443780d635ccc5e3564John Hoford        // TODO should save and Image
1495b539461dcc159bd89297443780d635ccc5e3564John Hoford    }
1505b539461dcc159bd89297443780d635ccc5e3564John Hoford
1515b539461dcc159bd89297443780d635ccc5e3564John Hoford    public void looksClick(View v) {
1525b539461dcc159bd89297443780d635ccc5e3564John Hoford        String[] looks = mVrView.getLooks();
1535b539461dcc159bd89297443780d635ccc5e3564John Hoford        PopupMenu popup = new PopupMenu(this, v);
1545b539461dcc159bd89297443780d635ccc5e3564John Hoford        Menu menu = popup.getMenu();
1555b539461dcc159bd89297443780d635ccc5e3564John Hoford
1565b539461dcc159bd89297443780d635ccc5e3564John Hoford        for (int i = 0; i < looks.length; i++) {
1575b539461dcc159bd89297443780d635ccc5e3564John Hoford            menu.add(0, Menu.FIRST + i, Menu.NONE, looks[i]);
1585b539461dcc159bd89297443780d635ccc5e3564John Hoford        }
1595b539461dcc159bd89297443780d635ccc5e3564John Hoford
1605b539461dcc159bd89297443780d635ccc5e3564John Hoford        //registering popup with OnMenuItemClickListener
1615b539461dcc159bd89297443780d635ccc5e3564John Hoford        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
1625b539461dcc159bd89297443780d635ccc5e3564John Hoford            public boolean onMenuItemClick(MenuItem item) {
1635b539461dcc159bd89297443780d635ccc5e3564John Hoford                mVrView.setLook(item.getTitle().toString());
1645b539461dcc159bd89297443780d635ccc5e3564John Hoford                return true;
1655b539461dcc159bd89297443780d635ccc5e3564John Hoford            }
1665b539461dcc159bd89297443780d635ccc5e3564John Hoford        });
1675b539461dcc159bd89297443780d635ccc5e3564John Hoford        popup.show();
1685b539461dcc159bd89297443780d635ccc5e3564John Hoford        uncheckOthers(v);
1695b539461dcc159bd89297443780d635ccc5e3564John Hoford    }
1705b539461dcc159bd89297443780d635ccc5e3564John Hoford
1715b539461dcc159bd89297443780d635ccc5e3564John Hoford    public void dataClick(View v) {
1725b539461dcc159bd89297443780d635ccc5e3564John Hoford        Log.v(LOGTAG, "dataClick");
1735b539461dcc159bd89297443780d635ccc5e3564John Hoford
1745b539461dcc159bd89297443780d635ccc5e3564John Hoford        String[] volumes = mLoader.getNames();
1755b539461dcc159bd89297443780d635ccc5e3564John Hoford        PopupMenu popup = new PopupMenu(this, v);
1765b539461dcc159bd89297443780d635ccc5e3564John Hoford        Menu menu = popup.getMenu();
1775b539461dcc159bd89297443780d635ccc5e3564John Hoford
1785b539461dcc159bd89297443780d635ccc5e3564John Hoford        for (int i = 0; i < volumes.length; i++) {
1795b539461dcc159bd89297443780d635ccc5e3564John Hoford            menu.add(0, Menu.FIRST + i, Menu.NONE, volumes[i]);
1805b539461dcc159bd89297443780d635ccc5e3564John Hoford        }
1815b539461dcc159bd89297443780d635ccc5e3564John Hoford
1825b539461dcc159bd89297443780d635ccc5e3564John Hoford        //registering popup with OnMenuItemClickListener
1835b539461dcc159bd89297443780d635ccc5e3564John Hoford        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
1845b539461dcc159bd89297443780d635ccc5e3564John Hoford            public boolean onMenuItemClick(MenuItem item) {
1855b539461dcc159bd89297443780d635ccc5e3564John Hoford
1865b539461dcc159bd89297443780d635ccc5e3564John Hoford                VrSetupTask setup = new VrSetupTask();
1875b539461dcc159bd89297443780d635ccc5e3564John Hoford                String title = item.getTitle().toString();
1885b539461dcc159bd89297443780d635ccc5e3564John Hoford                TextView tv = (TextView) findViewById(R.id.title);
1895b539461dcc159bd89297443780d635ccc5e3564John Hoford                tv.setText(title);
1905b539461dcc159bd89297443780d635ccc5e3564John Hoford                setup.execute(title);
1915b539461dcc159bd89297443780d635ccc5e3564John Hoford                return true;
1925b539461dcc159bd89297443780d635ccc5e3564John Hoford            }
1935b539461dcc159bd89297443780d635ccc5e3564John Hoford        });
1945b539461dcc159bd89297443780d635ccc5e3564John Hoford
1955b539461dcc159bd89297443780d635ccc5e3564John Hoford        popup.show();
1965b539461dcc159bd89297443780d635ccc5e3564John Hoford        uncheckOthers(v);
1975b539461dcc159bd89297443780d635ccc5e3564John Hoford    }
1985b539461dcc159bd89297443780d635ccc5e3564John Hoford
1995b539461dcc159bd89297443780d635ccc5e3564John Hoford    private void uncheckOthers(View v) {
2005b539461dcc159bd89297443780d635ccc5e3564John Hoford        ViewGroup p = (ViewGroup) v.getParent().getParent();
2015b539461dcc159bd89297443780d635ccc5e3564John Hoford        uncheckOthers(p, v);
2025b539461dcc159bd89297443780d635ccc5e3564John Hoford    }
2035b539461dcc159bd89297443780d635ccc5e3564John Hoford
2045b539461dcc159bd89297443780d635ccc5e3564John Hoford    private void uncheckOthers(ViewGroup p, View v) {
2055b539461dcc159bd89297443780d635ccc5e3564John Hoford        int n = p.getChildCount();
2065b539461dcc159bd89297443780d635ccc5e3564John Hoford        for (int i = 0; i < n; i++) {
2075b539461dcc159bd89297443780d635ccc5e3564John Hoford            final View child = p.getChildAt(i);
2085b539461dcc159bd89297443780d635ccc5e3564John Hoford            if (child instanceof ViewGroup) {
2095b539461dcc159bd89297443780d635ccc5e3564John Hoford                uncheckOthers((ViewGroup) child, v);
2105b539461dcc159bd89297443780d635ccc5e3564John Hoford            }
2115b539461dcc159bd89297443780d635ccc5e3564John Hoford            if (v != child && child instanceof ToggleButton) {
2125b539461dcc159bd89297443780d635ccc5e3564John Hoford                ToggleButton t = (ToggleButton) child;
2135b539461dcc159bd89297443780d635ccc5e3564John Hoford                t.setChecked(false);
2145b539461dcc159bd89297443780d635ccc5e3564John Hoford
2155b539461dcc159bd89297443780d635ccc5e3564John Hoford            }
2165b539461dcc159bd89297443780d635ccc5e3564John Hoford        }
2175b539461dcc159bd89297443780d635ccc5e3564John Hoford    }
2185b539461dcc159bd89297443780d635ccc5e3564John Hoford}
219