/// <summary>
        /// Query lock status
        /// </summary>
        /// <returns></returns>
        public bool IsAdmin()
        {
            DevicePolicyManager devicePolicyManager = (DevicePolicyManager)Application.Context.GetSystemService(Context.DevicePolicyService);
            ComponentName       mDeviceAdminRcvr    = new ComponentName(Application.Context, Java.Lang.Class.FromType(typeof(DeviceAdminReceiverClass)).Name);

            return(devicePolicyManager.IsAdminActive(mDeviceAdminRcvr));
        }
Beispiel #2
0
        public static void BecomeHomeActivity(Context c)
        {
            ComponentName       deviceAdmin = new ComponentName(c, "AdminReceiver");
            DevicePolicyManager dpm         = (DevicePolicyManager)c.GetSystemService(Context.DevicePolicyService);

            dpm.SetLockTaskPackages(deviceAdmin, new string[] { "br.com.AndroidTest" });

            if (!dpm.IsAdminActive(deviceAdmin))
            {
                showToast(c, "This app is not a device admin!");
                return;
            }
            if (!dpm.IsDeviceOwnerApp(c.PackageName))
            {
                showToast(c, "This app is not the device owner!");
                return;
            }
            IntentFilter intentFilter = new IntentFilter(Intent.ActionMain);

            intentFilter.AddCategory(Intent.CategoryDefault);
            intentFilter.AddCategory(Intent.CategoryHome);
            ComponentName activity = new ComponentName(c, "MainActivity");

            dpm.AddPersistentPreferredActivity(deviceAdmin, intentFilter, activity);
            showToast(c, "Home activity: " + GetHomeActivity(c));
        }
        public static bool IsThisAppADeviceAdministrator()
        {
            DevicePolicyManager devicePolicyManager = Application.Context.GetSystemService(Context.DevicePolicyService) as DevicePolicyManager;

            ComponentName componentName = new ComponentName(Application.Context, Java.Lang.Class.FromType(typeof(AdminReceiver)));

            return(devicePolicyManager.IsAdminActive(componentName));
        }
Beispiel #4
0
        public override DeviceCheckResult Check()
        {
            DevicePolicyManager policyManager = (DevicePolicyManager)context
                                                .GetSystemService(Context.DevicePolicyService);
            bool enabled = policyManager != null && policyManager
                           .StorageEncryptionStatus == EncryptionStatus.Active;

            return(new DeviceCheckResult(this, enabled));
        }
        public void SetAsDeviceAdmin(Context context)
        {
            DevicePolicyManager devicePolicyManager = (DevicePolicyManager)context.GetSystemService(Context.DevicePolicyService);
            ComponentName       deviceAdmin         = new ComponentName(context, Java.Lang.Class.FromType(typeof(DeviceAdmin)));
            Intent intent = new Intent(DevicePolicyManager.ActionAddDeviceAdmin);

            intent.PutExtra(DevicePolicyManager.ExtraDeviceAdmin, deviceAdmin);
            intent.PutExtra(DevicePolicyManager.ExtraAddExplanation, "화면 잠금을 위해 기기 관리자 설정을 필요로 합니다.\n삭제할 때는 기기관리자를 해제해야 삭제가 가능합니다.");
            context.StartActivity(intent);
        }
Beispiel #6
0
        protected async override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Intent.AddFlags(Android.Content.ActivityFlags.ClearTop);

            DevicePolicyManager devicePolicyManager = (DevicePolicyManager)GetSystemService(DevicePolicyService);

            ActivityManager activityManager = (ActivityManager)GetSystemService(ActivityService);
            var             compName        = new ComponentName(this, Java.Lang.Class.FromType(typeof(DeviceAdminLock)).Name);

            Intent intent = new Intent(DevicePolicyManager.ActionAddDeviceAdmin);

            intent.PutExtra(DevicePolicyManager.ExtraDeviceAdmin, compName);
            intent.PutExtra(DevicePolicyManager.ExtraAddExplanation, "Additional text explaining why this needs to be added.");
            StartActivityForResult(intent, 1);

            devicePolicyManager.SetKeyguardDisabled(compName, true);

            //bool active = devicePolicyManager.IsAdminActive(compName);
            //if (active) {
            //    // if available then lock
            //    devicePolicyManager.RemoveActiveAdmin(compName);
            //}

            //KeyguardManager km = (KeyguardManager)GetSystemService(KeyguardService);
            //KeyguardLock kl = km.NewKeyguardLock(PackageName);
            //kl.DisableKeyguard();   // WORKS ONLY IN SERVICE ?

            //PowerManager pm = (PowerManager)GetSystemService(PowerService);
            //WakeLock wakeLock = pm.NewWakeLock(WakeLockFlags.Full
            //                                   | WakeLockFlags.AcquireCausesWakeup
            //                                   | WakeLockFlags.OnAfterRelease, "MyWakeLock");
            //wakeLock.Acquire();

            var time = DateTime.Now.ToShortTimeString();

            var textView = new TextView(this)
            {
                Text             = time,
                LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent)
            };

            var webView = new WebView(this);

            webView.SetWebViewClient(new WebViewClient());
            webView.Settings.JavaScriptEnabled = true;
            webView.LoadUrl("http://youtube.com");

            //SetContentView(textView);
            SetContentView(webView);
        }
        public void Lock(Context context)
        {
            DevicePolicyManager devicePolicyManager = (DevicePolicyManager)context.GetSystemService(Context.DevicePolicyService);
            ComponentName       deviceAdmin         = new ComponentName(context, Java.Lang.Class.FromType(typeof(DeviceAdmin)));

            if (!devicePolicyManager.IsAdminActive(deviceAdmin))
            {
                Intent intent = new Intent(context, typeof(DialogWidgetActivity));
                context.StartActivity(intent);
            }
            else
            {
                devicePolicyManager.LockNow();
            }
        }
        private void Lock(Context context)
        {
            DevicePolicyManager Policy = (DevicePolicyManager)context.GetSystemService(Context.DevicePolicyService);
            ComponentName       admin  = new ComponentName(context, "com.rizlas.buttonwidget.AdminReceiver");

            bool Active = Policy.IsAdminActive(admin);

            if (Active)
            {
                Policy.LockNow();
            }
            else
            {
                Toast.MakeText(context, "Please grant admin permissions", ToastLength.Long).Show();
            }
        }
Beispiel #9
0
        private void Lock(Context context)
        {
            DevicePolicyManager Policy = (DevicePolicyManager)context.GetSystemService(Context.DevicePolicyService);
            ComponentName       admin  = new ComponentName(context, "com.rizlas.buttonwidget.AdminReceiver");

            bool Active = Policy.IsAdminActive(admin);

            if (!Active)
            {
                Intent intent = new Intent(DevicePolicyManager.ActionAddDeviceAdmin);
                intent.PutExtra(DevicePolicyManager.ExtraDeviceAdmin, admin);
                intent.PutExtra(DevicePolicyManager.ExtraAddExplanation, "Permissions to lock the phone screen");
                context.StartActivity(intent);
            }
            else
            {
                Toast.MakeText(this, "Admin access granted", ToastLength.Long).Show();
            }
        }
 private void EnableDeviceAdmin_Click(object sender, EventArgs e)
 {
     if (Checkers.IsThisAppADeviceAdministrator())
     {
         ComponentName       devAdminReceiver = new ComponentName(Application.Context, Java.Lang.Class.FromType(typeof(AdminReceiver)));
         DevicePolicyManager dpm = (DevicePolicyManager)GetSystemService(Context.DevicePolicyService);
         dpm.RemoveActiveAdmin(devAdminReceiver);
     }
     else
     {
         using (AlertDialog.Builder builder = new AlertDialog.Builder(this))
         {
             builder.SetMessage(Resource.String.dialogfordeviceaccessdescription);
             builder.SetPositiveButton(Resource.String.dialogallowbutton, new EventHandler <DialogClickEventArgs>(OnDialogPositiveButtonEventArgs));
             builder.SetNegativeButton(Resource.String.dialogcancelbutton, null as EventHandler <DialogClickEventArgs>);
             builder.Show();
         }
     }
 }
Beispiel #11
0
 private void Timer_Elapsed(object sender, ElapsedEventArgs e)
 {
     try {
         DateTime dtNow = DateTime.Now;
         lngMiliseconds += 1000;
         if (dtNow >= dtEndTime)
         {
             timer.Stop();
             DevicePolicyManager dpmDeviceLocker = (DevicePolicyManager)GetSystemService(Context.DevicePolicyService);
             dpmDeviceLocker.LockNow();
             this.StopSelf();
         }
         else
         {
             double lngSecondsRemaining = (dtEndTime - dtNow).TotalMilliseconds;
             BroadCastMessage("RemainingSeconds", lngSecondsRemaining);
         }
     }
     catch (Exception ex)
     { Toast.MakeText(this, "Error:" + ex.Message, ToastLength.Long).Show(); }
 }
Beispiel #12
0
        private void Mylock(DevicePolicyManager policyManager, ComponentName componentName)
        {
            if (Build.VERSION.SdkInt > BuildVersionCodes.Froyo)
            {
                var policy = new StrictMode.ThreadPolicy.Builder().PermitAll().Build();
                StrictMode.SetThreadPolicy(policy);
            }
            var active = policyManager.IsAdminActive(componentName);

            if (!active)
            {                                // Without permission
                Log.Error(logTag, "No authority~");
                ActiveManage(componentName); // To get access
            }
            else
            {
                Log.Error(logTag, "Has authority");
            }
            policyManager.LockNow();     // lock screen directly
            Finish();
        }
        /// <summary>
        /// Request lock status
        /// </summary>
        /// <param name="status"></param>
        public void RequestAdmin(bool status)
        {
            DevicePolicyManager devicePolicyManager = (DevicePolicyManager)Application.Context.GetSystemService(Context.DevicePolicyService);
            ComponentName       mDeviceAdminRcvr    = new ComponentName(Application.Context, Java.Lang.Class.FromType(typeof(DeviceAdminReceiverClass)).Name);

            if (devicePolicyManager.IsAdminActive(mDeviceAdminRcvr))
            {
                devicePolicyManager.SetLockTaskPackages(mDeviceAdminRcvr, new String[] { Application.Context.PackageName });

                if (devicePolicyManager.IsLockTaskPermitted(Application.Context.PackageName))
                {
                    if (status)
                    {
                        MainActivity.MainApplicationActivity.StartLockTask();
                    }
                    else
                    {
                        MainActivity.MainApplicationActivity.StopLockTask();
                    }
                }
            }
        }
Beispiel #14
0
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            //Set interface language
            try {
                Lang = ReadPresestingString("Lang");
                SetInterfaceLocal(Lang, this.BaseContext);
                // Set our view from the "main" layout resource
                SetContentView(Resource.Layout.Main);

                brReciever    = new MessageRecever(this);
                intentService = new IntentFilter(Application.PackageName);

                //Assign textview of the selected numbers of minutes, and time remaining
                tvMinutes   = FindViewById <TextView>(Resource.Id.tvMinutes);
                tvRemaining = FindViewById <TextView>(Resource.Id.tvRemaining);
                //Set min and max of the seekbar of the time to wait before lock device
                int_MaxPeriod = Resources.GetInteger(Resource.Integer.maxPeriod);
                int_MinPeriod = Resources.GetInteger(Resource.Integer.minPeriod);
                sbSetPeriod   = FindViewById <SeekBar>(Resource.Id.sbSelectedMinutes);
                sbSetPeriod.ProgressChanged += SbSetPeriod_ProgressChanged;
                sbSetPeriod.Max              = int_MaxPeriod - int_MinPeriod;

                //Set the start button click procedure
                btnStart        = FindViewById <Button>(Resource.Id.btnStart);
                btnStart.Click += Start_Button_Click;

                //Using PolicyManager ask for admin privilige to be able to lock device, will ask once.
                DevicePolicyManager devicePolicyManager = (DevicePolicyManager)GetSystemService(Context.DevicePolicyService);
                ComponentName       cnDeviceAdmin       = new ComponentName(this, Java.Lang.Class.FromType(typeof(DeviceAdmin)));
                Intent intent = new Intent(DevicePolicyManager.ActionAddDeviceAdmin);
                intent.PutExtra(DevicePolicyManager.ExtraDeviceAdmin, cnDeviceAdmin);
                intent.PutExtra(DevicePolicyManager.ExtraAddExplanation, "To enable autolock functionality.");
                StartActivity(intent);
            }
            catch (Exception ex)
            { Toast.MakeText(this, "Error:" + ex.Message, ToastLength.Long).Show(); }
        }
Beispiel #15
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            // Looks like permissions to read and write external storage must be explicit.
            //https://stackoverflow.com/questions/31746787/xamarin-android-system-unauthorizedaccessexception-access-to-the-path-is-de
            if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.WriteExternalStorage) != (int)Permission.Granted)
            {
                ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.WriteExternalStorage }, 0);
            }

            if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.ReadExternalStorage) != (int)Permission.Granted)
            {
                ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.ReadExternalStorage }, 0);
            }

            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource   = Resource.Layout.Toolbar;

            base.OnCreate(savedInstanceState);

            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());

            // This will prompt the user to allow/disallow the elevation of this app as "device owner".
            DevicePolicyManager devicePolicyManager = (DevicePolicyManager)GetSystemService(Context.DevicePolicyService);
            ComponentName       demoDeviceAdmin     = new ComponentName(this, Java.Lang.Class.FromType(typeof(DeviceAdminBroadcastReceiver)));
            Intent intent = new Intent(DevicePolicyManager.ActionAddDeviceAdmin);

            intent.PutExtra(DevicePolicyManager.ExtraDeviceAdmin, demoDeviceAdmin);
            intent.PutExtra(DevicePolicyManager.ExtraAddExplanation, "Device administrator");
            StartActivity(intent);

            IntentFilter filter = new IntentFilter();

            filter.AddAction("ANY_UNIQUE_NAME_WILL_DO");
            RegisterReceiver(new InstallerStatusBroadcastReceiver(), filter);
        }
Beispiel #16
0
        protected override void OnCreate(Bundle bundle)
        {
            AppCenter.Start("da02ffa0-e4a9-4292-8423-aee119dd51b4", typeof(Analytics), typeof(Crashes));

            DeviceInformation = $"Model :{DeviceInfo.Model}, VersionString:{DeviceInfo.VersionString},Platform:{DeviceInfo.Platform},Version:{DeviceInfo.Version} , Idiom:{DeviceInfo.Idiom}";

            MainActivityInstance = this;

            devicePolicyManager = (DevicePolicyManager)GetSystemService(Context.DevicePolicyService);
            myDeviceAdmin       = new ComponentName(this, Java.Lang.Class.FromType(typeof(DeviceAdmin)));

            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource   = Resource.Layout.Toolbar;

            base.OnCreate(bundle);

            //////////////////////////////////////////

            AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledExceptionHandler;
            TaskScheduler.UnobservedTaskException      += TaskSchedulerOnUnobservedTaskExceptionHandler;

            //Forms.Init(this, bundle);
            //DisplayCrashReport();

            //////////////////////////////////////////

            IranSansFont = Typeface.CreateFromAsset(Assets, "IRANSansMobile.ttf");
            foreach (var font in new string[] { "DEFAULT", "MONOSPACE", "SERIF", "SANS_SERIF" })
            {
                FontsOverride.SetDefaultFont(font, IranSansFont);
            }

            Xamarin.FormsMaps.Init(this, bundle);

            Forms.Init(this, bundle);

            OxyPlot.Xamarin.Forms.Platform.Android.PlotViewRenderer.Init();

            App.imagesDirectory = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "StuffsGallery");

            InitializeSharedResources(this, ContentResolver);

            App.Downloader       = new Downloader();
            App.QRScanner        = new QRScan(this);
            App.Uploader         = new Uploader();
            App.BluetoothPrinter = new BluetoothPrinter(this);
            PersianDatePicker.FragmentManager = FragmentManager;
            App.PersianDatePicker             = new PersianDatePicker();

            App.DeviceSizeDensity = Resources.DisplayMetrics.Density;

            try
            {
                CreateMapIcons();
            }
            catch (Exception err)
            {
                Crashes.TrackError(err, GetDeviceInfoAsDic());
            }

            MessagingCenter.Subscribe <MainMenu>(this, "MainMenuOpened", StartService);

            LoadApplication(new App());

            Xamarin.Essentials.Platform.Init(this, bundle); // add this line to your code, it may also be called: bundle
            Analytics.TrackEvent("OnCreate", GetDeviceInfoAsDic());
        }
Beispiel #17
0
 internal DevicePolicy(DevicePolicyManager dpm)
 {
     _dpm = dpm;
 }