OSDN Git Service

ちょっとだけ、preferenceの準備。
[gokigen/FujiCam.git] / app / src / main / java / net / osdn / gokigen / cameratest / MainActivity.java
1 package net.osdn.gokigen.cameratest;
2
3 import com.google.android.material.floatingactionbutton.FloatingActionButton;
4
5 import androidx.annotation.NonNull;
6 import androidx.appcompat.app.AppCompatActivity;
7 import androidx.appcompat.widget.Toolbar;
8
9 import androidx.core.app.ActivityCompat;
10 import androidx.core.content.ContextCompat;
11 import androidx.viewpager.widget.ViewPager;
12
13 import android.Manifest;
14 import android.content.Intent;
15 import android.content.pm.PackageManager;
16 import android.os.Bundle;
17 import android.provider.Settings;
18 import android.util.Log;
19 import android.view.Menu;
20 import android.view.MenuItem;
21 import android.view.View;
22 import android.view.WindowManager;
23
24 import net.osdn.gokigen.cameratest.camtest.CamTest;
25 import net.osdn.gokigen.cameratest.pages.SectionsPagerAdapter;
26
27 import org.opencv.android.BaseLoaderCallback;
28 import org.opencv.android.LoaderCallbackInterface;
29
30 public class MainActivity extends AppCompatActivity implements IApplicationControl
31 {
32     /////// OpenCV ///////  : license https://opencv.org/license/
33     static
34     {
35         System.loadLibrary("opencv_java4");
36     }
37
38     private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this)
39     {
40         @Override
41         public void onManagerConnected(int status)
42         {
43             switch (status)
44             {
45                 case LoaderCallbackInterface.SUCCESS:
46                 {
47                     Log.i(TAG, "OpenCV loaded successfully");
48                 } break;
49                 default:
50                 {
51                     super.onManagerConnected(status);
52                 } break;
53             }
54         }
55     };
56
57     private final String TAG = toString();
58     private SectionsPagerAdapter mSectionsPagerAdapter;
59     private ViewPager mViewPager;
60     private CamTest testTarget;
61
62     /**
63      *
64      */
65     @Override
66     protected void onCreate(Bundle savedInstanceState)
67     {
68         super.onCreate(savedInstanceState);
69         setContentView(R.layout.activity_main);
70
71         testTarget = new CamTest(this);
72
73         Toolbar toolbar = findViewById(R.id.toolbar);
74         setSupportActionBar(toolbar);
75
76         getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
77
78         // Create the adapter that will return a fragment for each of the three
79         // primary sections of the activity.
80         mSectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager(), testTarget, this);
81
82         // Set up the ViewPager with the sections adapter.
83         mViewPager = findViewById(R.id.container);
84         mViewPager.setAdapter(mSectionsPagerAdapter);
85
86         FloatingActionButton fab = findViewById(R.id.fab);
87         fab.setOnClickListener(new View.OnClickListener()
88         {
89             @Override
90             public void onClick(View view)
91             {
92                 testTarget.connect();
93             }
94         });
95
96         // パーミッション群のオプトイン
97         final int REQUEST_NEED_PERMISSIONS = 1010;
98         if ((ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) ||
99                 (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_NETWORK_STATE) != PackageManager.PERMISSION_GRANTED) ||
100                 (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_WIFI_STATE) != PackageManager.PERMISSION_GRANTED) ||
101                 (ContextCompat.checkSelfPermission(this, Manifest.permission.VIBRATE) != PackageManager.PERMISSION_GRANTED) ||
102                 (ContextCompat.checkSelfPermission(this, Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED)) {
103             ActivityCompat.requestPermissions(this,
104                     new String[]{
105                             Manifest.permission.WRITE_EXTERNAL_STORAGE,
106                             Manifest.permission.ACCESS_NETWORK_STATE,
107                             Manifest.permission.ACCESS_WIFI_STATE,
108                             Manifest.permission.VIBRATE,
109                             Manifest.permission.INTERNET,
110                     },
111                     REQUEST_NEED_PERMISSIONS);
112         }
113         initializeClass();
114         prepareClass();
115         onReadyClass();
116     }
117
118     /**
119      *
120      */
121     @Override
122     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
123     {
124         super.onRequestPermissionsResult(requestCode, permissions, grantResults);
125         prepareClass();
126         onReadyClass();
127     }
128
129     /**
130      *
131      */
132     @Override
133     public boolean onCreateOptionsMenu(Menu menu)
134     {
135         // Inflate the menu; this adds items to the action bar if it is present.
136         getMenuInflater().inflate(R.menu.menu_main, menu);
137         return true;
138     }
139
140     /**
141      *
142      */
143     @Override
144     public boolean onOptionsItemSelected(MenuItem item)
145     {
146         // Handle action bar item clicks here. The action bar will
147         // automatically handle clicks on the Home/Up button, so long
148         // as you specify a parent activity in AndroidManifest.xml.
149         int id = item.getItemId();
150
151         if (id == R.id.action_wifi_settings)
152         {
153             try
154             {
155                 // Wifi 設定画面を表示する
156                 startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
157                 return (true);
158             }
159             catch (Exception e)
160             {
161                 e.printStackTrace();
162             }
163             return (true);
164         }
165         if (id == R.id.action_exit)
166         {
167             // アプリケーションを終了する
168             exitApplication();
169             return (true);
170         }
171         if (id == R.id.action_disconnect)
172         {
173             testTarget.disconnect();
174             return (true);
175         }
176 /*
177         if (id == R.id.action_settings)
178         {
179             testTarget.settings();
180             return (true);
181         }
182         if (id == R.id.action_reset)
183         {
184             testTarget.resetConnection();
185             return (true);
186         }
187         //noinspection SimplifiableIfStatement
188         if (id == R.id.action_up_value)
189         {
190             testTarget.valueUp();
191             return (true);
192         }
193         if (id == R.id.action_down_value)
194         {
195             testTarget.valueDown();
196             return (true);
197         }
198 */
199         return super.onOptionsItemSelected(item);
200     }
201
202     /**
203      * クラスの初期化 (instantiate)
204      */
205     private void initializeClass()
206     {
207         try
208         {
209             Log.v(TAG, "Initialize ...");
210
211
212         }
213         catch (Exception e)
214         {
215             e.printStackTrace();
216         }
217     }
218
219     /**
220      * クラスの準備 (prepare)
221      */
222     private void prepareClass()
223     {
224         try
225         {
226             Log.v(TAG, "Prepare ...");
227         }
228         catch (Exception e)
229         {
230             e.printStackTrace();
231         }
232     }
233
234     /**
235      * クラスの準備完了 (onReady)
236      */
237     private void onReadyClass()
238     {
239         try
240         {
241             Log.v(TAG, "on Ready ...");
242         }
243         catch (Exception e)
244         {
245             e.printStackTrace();
246         }
247     }
248
249     /**
250      *
251      */
252     @Override
253     public void exitApplication()
254     {
255         try
256         {
257             testTarget.disconnect();
258             finish();
259         }
260         catch (Exception e)
261         {
262             e.printStackTrace();
263         }
264     }
265
266     /**
267      *
268      */
269     @Override
270     protected void onResume()
271     {
272         super.onResume();
273
274         Log.d(TAG, "OpenCV library found inside package. Using it!");
275         mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
276     }
277
278     /**
279      *
280      */
281     @Override
282     protected void onPause()
283     {
284         super.onPause();
285     }
286
287 }